Back

Twitter Ads API Essential Guide

Aug 9, 20246 minute read

What type of API does Twitter Ads provide?

Twitter Ads API Type

  • Twitter Ads has a REST API.

Key Features of the Twitter Ads REST API

  • It allows programmatic creation, scheduling, and management of ad campaigns on Twitter.
  • It provides campaign management capabilities.
  • It enables working with ad creatives.

REST API Characteristics

  • REST (Representational State Transfer) APIs use HTTP methods like GET, POST, PUT, DELETE to perform operations.
  • They typically use JSON for data exchange.
  • REST APIs are stateless and use standard HTTP status codes for error handling.
  • They are widely used for public APIs due to their simplicity and ease of use.

Comparison to Other API Types

  • While Twitter Ads uses REST, other API types like SOAP and GraphQL exist.
  • REST is generally considered simpler and more lightweight than SOAP.
  • GraphQL provides more flexibility in data querying compared to REST, but REST remains very popular for public APIs.

Does the Twitter Ads API have webhooks?

Webhooks in Twitter Ads API

The official Twitter Ads API does not appear to have dedicated webhooks specifically for advertising events.

Account Activity API Webhooks

However, Twitter does offer webhooks through its Account Activity API, which is separate from the Ads API. The Account Activity API provides webhook functionality for various account-related events. Here are some key points about these webhooks:

  1. The Account Activity API is a webhook-based API that sends account events to a web app you develop, deploy, and host.

  2. To use these webhooks, you need to:

    • Create a Twitter app
    • Obtain Account Activity API access
    • Develop a web app that consumes webhook events
  3. You can subscribe to receive events for specific Twitter accounts.

Types of Events

While the search results don't provide an exhaustive list of event types, they do mention a few examples:

  1. Favorite events: You can receive a favorite_events webhook when a user favorites a post.

  2. Direct Message events: All incoming Direct Messages are delivered via webhooks.

  3. The webhook events include a for_user_id field that indicates which subscription the event was delivered for.

Important Considerations

  • Webhook URLs are immutable. To change a webhook URL, you must delete the existing webhook and create a new one.
  • When you create a new webhook, you'll need to re-add user subscriptions to it.
  • Your webhook may receive duplicate events in some cases, so your app should be able to handle this.

Setting Up Webhooks

The process of setting up webhooks involves:

  1. Registering your webhook URL with Twitter
  2. Implementing a Challenge Response Check (CRC) in your web app
  3. Adding subscriptions for the accounts you want to receive events for

It's worth noting that the search results primarily discuss the Account Activity API webhooks, not specific Ads API webhooks. If you need advertising-specific event data, you may need to use the Ads API's analytics endpoints in combination with these account activity webhooks or explore other solutions.

Rate Limits and other limitations

Here are the key points about the API rate limits for the Twitter Ads API:

Types of Rate Limits

  • There are two types of rate limits:

    1. User token level
    2. Ad account level
  • Only a subset of endpoints use ad account level rate limiting [1].

Rate Limit Headers

  • User level rate limits are expressed via these headers:

    • x-rate-limit-limit
    • x-rate-limit-remaining
    • x-rate-limit-reset
  • Ad account level rate limits (when applicable) are expressed via:

    • x-account-rate-limit-limit
    • x-account-rate-limit-remaining
    • x-account-rate-limit-reset [1]

Ad Account vs User Level Limits

  • Ad account level limits are only provided for GET endpoints to enable syncing entity data across multiple ad accounts from a single user token.

  • Write actions are not guaranteed to use ad account level rate limits.

  • When available, the ad account level limit should take precedence over the user level limit [1].

Best Practices

  • Save timestamps of last synced data to avoid redundant syncing
  • Request multiple entities in a single call when possible
  • Use maximum "count" parameter values in requests
  • Implement exponential backoff and retry logic
  • Cache frequently accessed data
  • Batch process requests where possible
  • Prioritize important API endpoints

Key Points

  • Rate limits cannot be bypassed - attempting to do so can result in restrictions
  • Limits are enforced to maintain stability and prevent abuse
  • Developers should optimize code and respect Twitter's limits

In summary, the Twitter Ads API uses a combination of user and ad account level rate limits, with specific headers to track usage. Following best practices around efficient API usage is important to work within the defined limits.

Latest API Version

The most recent version of the Twitter Ads API is version 12.0.

Key points to consider:

  1. Version 12.0 was introduced on October 27, 2022 [1].

  2. Twitter (now X) regularly updates its Ads API, with new versions typically released every 6-12 months [1].

  3. There is usually a 6-month transition period between versions, during which the previous version remains available [1].

  4. As of the latest information provided, versions 11.0, 10.0, and 9.0 are still active, with their end-of-life dates not yet determined [1].

  5. Developers are encouraged to migrate to the latest version as soon as possible to avoid service disruptions and to access the newest features [1].

Best practices:

  1. Always use the most recent version of the API when developing new applications or features.

  2. Regularly check the Twitter Ads API documentation for updates and new version announcements.

  3. Plan for migrations well in advance of the end-of-life date for the version you're currently using.

  4. Be aware of breaking changes between versions and adjust your code accordingly during migrations.

  5. Take advantage of new features and improvements introduced in each version to optimize your advertising campaigns and analytics.

How to get a Twitter Ads developer account and API Keys?

Step 1: Sign up for a Twitter Developer Account

  1. Go to the Twitter Developer Portal (developer.twitter.com) and sign up for a developer account if you don't already have one.

  2. During the signup process, you'll need to create a Project and an associated developer App. This will provide you with a set of credentials that you'll use to authenticate API requests.

Step 2: Save Your App's Keys and Tokens

Once you've created your App, you'll receive the following credentials:

  • API Key and Secret
  • Access Token and Secret
  • Client ID and Client Secret
  • App-only Access Token

Make sure to save these securely, as they will only be displayed once.

Step 3: Apply for Ads API Access

  1. Visit ads.x.com/help to submit a request for Ads API access for each of your developer app(s).

  2. As part of the application process, you'll need to specify what level of access you require. Learn about App-level and Ad Account-level permissions.

  3. You'll need to provide information about your use case for the Twitter Ads API.

Step 4: Wait for Approval

After submitting your request, you'll need to wait for Twitter to review and approve your application for Ads API access.

Step 5: Set Up Your Integration

Once approved:

  1. Regenerate any user access tokens to make properly authenticated Ads API requests.

  2. Read through the Ads API documentation, including versioning and hierarchy.

  3. Explore the various Ads API tools and libraries to aid in your integration.

What can you do with the Twitter Ads API?

Based on the search results provided, here is a list of data models that can be interacted with using the Twitter Ads API, along with what is possible for each:

Campaign Management

  • Programmatically create, schedule, and manage ad campaigns
  • Engage people on Twitter through targeted advertising

Advertising Analytics

  • Retrieve granular insights on ad campaign performance
  • Access a full range of metrics through both asynchronous and synchronous API endpoints

Custom Audiences

  • Create and manage Tailored Audiences
  • Use Twitter, web, or mobile data to build custom audience segments

Creatives

  • Draft and publish Tweets
  • Extend Tweet functionality with Cards
  • Manage and upload images and videos

General Data Access

  • Access core Twitter elements like Posts, Direct Messages, Spaces, Lists, users, etc.
  • Retrieve user profile data
  • Read timelines

Streaming Data

  • Access real-time data streams based on criteria like keywords or geographic location

Filtered Data

  • Apply filters to constrain data sets by hashtags, mentions, keywords, etc.
  • Specify which fields to include in API responses (e.g. tweet IDs, timestamps, user attributes)

Large Dataset Handling

  • Use pagination/cursoring to navigate through large result sets

Key points to consider:

  • The API uses OAuth 1.0a for secure authentication
  • Data is primarily returned in JSON format for easy parsing
  • Rate limits apply to different endpoints and should be managed carefully
  • Multiple access tiers are available with different capabilities and limits

The Twitter Ads API provides programmatic access to a wide range of advertising and analytics capabilities, allowing developers to build sophisticated tools for managing ad campaigns and analyzing their performance on the platform.