Skip to main content

Python TikTok Events API

This library is an unoffical Python wrapper arround the TikTok Events API, allowing for easy interaction with the API:

  • Pydantic types are used to validate the data to be sent to TikTok.
  • Customer identifiable information is hashed using SHA256 before being sent to the TikTok API.
  • Events can be sent one at a time or in batches.

The TikTok Events API allows advertisers to share the actions customers take on their websites and offiline directly with TikTok. This allows advertisers to measure the effectiveness of their TikTok campaigns and optimize their ad spend.

Please reference the TikTok Events API documentation for more information on the API and the data it accepts and requires: https://ads.tiktok.com/marketing_api/docs?id=1741601162187777.

TikTok API uses custom return error codes. Reference this TikTok Events API documentation for more information on the error codes.

Disclaimers

  1. This library is in beta. Feedback and contributions are welcome.

Installing

You can install pytt_events_api using pip:

pip install pytt-events-api

Quick Start Guide

Here's a quick guide on how to use the library to send events to TikTok.

Authentication

The only thing you need to do to authenticate with TikTok Events API is to set the environment variables listed below.

  • TIKTOK_ACCESS_TOKEN: Events API access token - Required
  • TIKTOK_PIXEL_ID: The ID of the pixel - Required
  • TIKTOK_API_VERSION: The API version to use, defaults to v1.3.
  • TIKTOK_TEST_EVENT_CODE: Used so events can be tested without affecting the pixel's data. You may find the test event code in the events manager under the "Test Events" tab.

You can find the values for these variables in the TikTok Events Manager.

The environment variables are loaded when the TikTokAuth class is initialized. If the environment variables are not found, the class will raise an exception.

Importing the library and initializing the API

from pytt_events.auth import TikTokAuth
from pytt_events.tiktok_events_api import TikTokEventsApi
from pytt_events.event import Event
from pytt_events.properties import Properties
from pytt_events.context import Context, Ad, Page, User
from pytt_events.properties import ContentType
from pytt_events.properties import Content

api = TikTokEventsApi()
auth = TikTokAuth()

Creating an event

context = Context(
    user_agent='Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.109 Safari/537.36',
    ip='13.57.97.131',
    ad=Ad(callback='E.C.P.v3fQ2RHacdkfKfofPmlyuStIIHJ4Af1tKYxF9zz2c2PLx1Oaw15oHpcfl5AH' ), # ttclid
    page=Page(
        url='https://www.example.com',
        referrer='https://www.google.com'
    ),
    user=User(
        external_id='123456',
        email='test@test.com',
        phone_number='+5541998862934',
        ttp='94e2a4j9-h3ss-k2h5-98cc-c84a745mk098',
    ))
properties = Properties(
    currency='BRL', # ISO 4217
    value=1.00,
    description='mock description',
    query='mock query',
    status='mock status',
    contents=[Content(
        content_type=ContentType.PRODUCT,
        content_id='123456789',
        content_name='mock content name',
        content_category='mock content category',
        price=1.00,
        quantity=1
    )]
)
event = Event (
    pixel_code=auth.TIKTOK_PIXEL_ID,
    test_event_code=auth.TIKTOK_TEST_EVENT_CODE,
    event='ViewContent',
    event_id='123456789',
    timestamp='2023-02-01T00:00:00-03:00', # str or datetime object
    context=context,
    properties=properties
)

Errors in the Docs

TikTok's documentation says that content_type should be a parameter of the Properties object, but it actually is a parameter of the Content object.

Sending an event

response = api.post_event(
    event=event,
    auth=auth
)

Sending events in bulk

events = []
response = api.post_events_in_bulk(events=events, auth=auth)

Metadata

Release files for pytt-events-api 0.1.10

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytt-events-api 0.1.10
File Size Uploaded
pytt_events_api-0.1.10.tar.gz 12.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytt-events-api 0.1.10
File Interpreter ABI Platform
pytt_events_api-0.1.10-py3-none-any.whl Python 3 none any Details

Total release size: 26.3 kB

Release files / pytt_events_api-0.1.10.tar.gz

Download URL pytt_events_api-0.1.10.tar.gz
Size 12.2 kB
Tags Source
SHA-256 checksum
How to use checksums
adaeb897f0c002e4fe1b252866e4b789279dac080097016aeda851bc477c5a1b
BLAKE2b-256 checksum
How to use checksums
253c90c0f471574ea0e000a9e1f649cc2a785e90bcff259d4cbb47850a0a0f7c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 colorama/0.4.4 importlib-metadata/4.6.4 keyring/23.5.0 pkginfo/1.8.2 readme-renderer/34.0 requests-toolbelt/0.9.1 requests/2.25.1 rfc3986/1.5.0 tqdm/4.57.0 urllib3/1.26.5 CPython/3.10.12

Release files / pytt_events_api-0.1.10-py3-none-any.whl

Download URL pytt_events_api-0.1.10-py3-none-any.whl
Size 14.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3c582d818edd2d0aa1af487be17fb22252c47aca450da8c9334eab893bd1a79f
BLAKE2b-256 checksum
How to use checksums
3eaf988cb4307b6e0d557631ffc5a63f32396ee3d7f3250fffab808f30a2b327
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 colorama/0.4.4 importlib-metadata/4.6.4 keyring/23.5.0 pkginfo/1.8.2 readme-renderer/34.0 requests-toolbelt/0.9.1 requests/2.25.1 rfc3986/1.5.0 tqdm/4.57.0 urllib3/1.26.5 CPython/3.10.12

Release history Release notifications | RSS feed

This release

0.1.10 This release

2 release files

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page