You can write job results in Treasure Data directly to your Facebook Custom Audience.
When you upload or create a Custom Audience, Facebook requires that you accept Custom Audience Terms. You see a notification when you export an ad object targeting a customer file or export value-based Custom Audiences. You receive subsequent reminders about Facebook Custom Audience terms every 90 days. For more information, see Better Facebook Ads ROI with Data-Driven Custom Audiences.
For sample workflows on how to write job results to Facebook Custom Audience, view Treasure Boxes.
- Basic knowledge of Treasure Data, including the TD Toolbelt.
- A Facebook Ad Account.
- Authorized Treasure Data Facebook app access to a Facebook Ad Account
- Admin or Advertiser is required as ad account role
- Optionally, Access Token and App Secret
This section is optional if you use an OAuth authentication.
Log in to Facebook Business Manager and create a system user.
Select the new system user and generate a new token for that user.
Treasure Data recommends that you generate a "never-expire" token.

- Grant the following permissions:
ads_management
ads_read
pages_manage_ads
pages_manage_metadata
pages_read_engagement
pages_read_user_content
read_insights
If you are not able to generate an access token to connect to Facebook, see System Users on Business Management APIs.
Before sending data to Facebook, the data must be hashed with SHA256 with no salt (is random data that is used as an additional input to a one-way function that hashes data).This requirement applies to all data except External Identifiers, App User IDs, and Page Scoped User IDs.
If you choose to prepare your data (normalization and hashing) before sending it to the connector, you can set the option No need to normalize and hash records to True.
Treasure Data result output normalizes your values automatically according to Facebook’s normalizing rules.
All values, uploaded to Facebook for matching, must be normalized using the normalizing rules of Facebook. Otherwise, the values miss chances to match (if they are not normalized). If you need to normalize the data manually, apply your own normalization before attempting to generate output results.
Learn more about customer information values passed to parameters for Facebook's Marketing API.
According to the data type, the following conversion behavior is applied while creating the resulting output.
| Parameter | Description |
|---|---|
| EXTERN_ID (External ID) | No action is needed. |
| PAGEUID | No action is needed. |
| MADID (Mobile advertiser ID) | Convert all characters to lowercase, keep hyphens. |
| EMAIL (Email addresses) | Trimming leading and trailing whitespace and convert all characters to lowercase. Learn more about Meta's requirements for hashing and normalization. |
| PHONE (Phone numbers) | Removing any non-digit characters and leading zeros. |
| GEN (Gender) | Trimming leading and trailing whitespace and convert all characters to lowercase. If the result does not match with “m” or “f”, an empty string is used (because it’s an invalid value, it is futile to attempt a match). |
| DOBY (Birth Year) | Removing any non-digit characters and get the first 4 digits. |
| DOBM (Birth Month) and DOBD (Birthday) | Removing any non-digit characters and get the first 2 digits. |
| FN (First Name) and LN (Last Name) | Trimming leading and trailing whitespace and convert all characters to lowercase. Remove all punctuation. Supports special characters in UTF-8 encoding. |
| FI (First Initial) | Apply the same as First Name and get the first character. |
| CT (City) | Convert all characters to lowercase and remove any non-alphabetic (a-z) characters. |
| ST (States) | Convert all characters to lowercase and remove any non-alphanumeric (a-z and 0-9) characters. If your value is US State, use a 2-character ANSI abbreviation code, the Treasure Data platform does not cut off input string (into 2 characters), because of the need to support states outside the US. |
| ZIP (Zip code) | Trimming leading and trailing whitespace and convert all characters to lowercase, remove any non-alphanumeric or whitespace from the result. If your value is US zip code, use exactly 5 digits, the Treasure Data platform does not cut off input string (into 5 characters), because of the need to support UK zip code format. |
| LOOKALIKE_VALUE | Long or double value; a null or empty value will be sent as 0. The column is supported by value-based Custom Audience only. |
In Treasure Data, you must create and configure the data connection prior to running your query. As part of the data connection, you provide authentication to access the integration.
An authentication for Facebook Custom Audience needs to be established before exporting your data to the target platform. Here are the steps to create an authentication.
Open TD Console.
Navigate to Integrations Hub > Catalog.
Search for and select Facebook Custom Audiences.

Select Create Authentication.
For Authentication Method select either Access Token or Oath.

Access Token: Enter Access Token and App Secret.
Oauth: Select an existing OAuth connection for Facebook Custom Audiences, or select the Click here link underOAuth connection to create a new connection. You will be directed to log in to your Facebook account and grant access to the Treasure Data connector application.
Allow fetching Ads Account: Enable this options to choose Ads Account from pre-populated dropdown in activation creation
After being redirected back to the Catalog. choose your new OAuth connection.
Select Continue.
Name the connection.
Select Done.
Column names are case-insensitive. For example, you can use email or EMAIL.
Output results need to follow the Facebook User schema. Supported column names are:
- EXTERN_ID: External ID
- MADID: Mobile advertiser ID
- EMAIL: Email addresses
- PHONE: Phone numbers
- GEN: Gender
- DOBY: Birth Year
- DOBM: Birth Month
- DOBD: Birthday
- LN: Last Name
- FN: First Name
- FI: First Initial
- CT: City
- ST: States
- ZIP: Zip code
- COUNTRY: Country code
- PAGEUID: Page scope user id
- LOOKALIKE_VALUE: value for value-based Custom Audience
If none of the supporting columns are found in the query result, you receive an error.
- Complete the instructions in Creating a Destination Integration.
- Navigate to Data Workbench > Queries.
- Select a query for which you would like to export data.
- Run the query to validate the result set.
- Select Export Results.
- Select an existing integration authentication.

- Define any additional Export Results details. In your export integration content review the integration parameters. For example, your Export Results screen might be different, or you might not have additional details to fill out:

- Select Done.
- Run your query.
- Validate that your data moved to the destination you specified.
| Parameter | Description |
|---|---|
| Ad Account ID (required) | This is your Ad Account ID without act_ prefix. API Parameter: ad_account_id |
| Action | You choose to Add, Replace or Remove users from Custom Audience. Note: Facebook does not allow removing users from a low Audience size (less than 1000 users), or removing users will result in a low Audience size, and you will receive an error You cannot remove users from this audience because it will result in a low audience size. API Parameter:action (ADD_USERS or REMOVE_USERS, Default: ADD_USER) |
| Custom Audience Name (required) | Important note: If you have many Custom Audiences with the same name as this input, the latest Custom Audience Name will be used. We recommend that you give your Custom Audience a unique name. - Add: Creates a new Custom Audience with a given name, if a Custom Audience with the given name does not exist. The exported result is added (appended) to the target audience. - Replace: Replace the records in the Custom Audience with the given name with the new uploaded records. One job replacing a custom audience can be in process at any time. After the job finishes on the TD side, it takes some additional time on the Instagram side. During that time other users can't replace that custom audience. - Remove: Leave this field blank will remove users from all Custom Audiences, otherwise will remove users from this name. API parameter: output_name |
| Custom Audience Description | Optional description of Custom Audience. API parameter: output_description |
| No need to normalize and hash records (default false) | Indicates whether the data has already been normalized and hashed. If not, TD automatically normalizes and hashes the records. API param: pre_hashed |
| Is Value Based? | Create or update a Value-Based Custom Audience. This field is required when your data has LookAlike_Value column |
| Page ID | Your Facebook page used to collect Page scope user id. This field is required when you add or remove users by specifying pageuid column. API param: page_id |
| Source of customer data | Specify the source of the user information collected into this file. This field is required when Action is Add users and the Custom Audience Name does not exist. API param: customer_file_source (default: null, values: USER_PROVIDED_ONLY, PARTNER_PROVIDED_ONLY, BOTH_USER_AND_PARTNER_PROVIDED) |
| Initial intervals in milliseconds between retries (optional, default 60000) | Interval to retry if a recoverable error occurs (in millisecond). |
| Retry limit (optional, default 5) | The number of retries before attempts end. |
| Sending large audiences | Enable this option to send large users to the audience. The default is false (only for the Replace action). Default: False. |

On Audience Studio's Activation Creation/Edit screen, the Ad Account ID field now features a convenient dropdown menu instead of manual input. When users select on the field, it automatically displays a list of Facebook Ad accounts they have access to. Each entry in the dropdown shows both the Ad Account ID and its associated name (e.g., "106640720131327 - Nhien An") for easy identification. Users can simply click on their desired account from the list rather than typing the ID manually, reducing input errors and improving the user experience.
You can use an alias in your query to rename columns of your query result. For example:
SELECT
an_email_column AS EMAIL,
another_phone_column AS PHONE
FROM your_table;Here is an example Audiences list before the output of a query result:

From Treasure Data, run the following query with Output results into a connection of Facebook Custom Audience:
SELECT email, fn, ln FROM (
VALUES ('demo1@example.com', 'John', 'Doe'),
('demo2@example.com', 'Isaac', 'Miceli'),
('demo3@example.com', 'Christopher', 'Agar')
) tbl (email, fn, ln)or for Page scope user ID
SELECT
pageuid
FROM (
VALUES('2018728488162687') ,
('1785406501565085') ,
('2341287802551031') ,
('2487019361340533') ,
('1093039407464499')
) tbl (pageuid)The query will not match any real users, it’s for demo purposes only. Also, the query requires no source table (for the ease of testing out this feature), but you still must choose a database, so pick “sample_datasets” or any other arbitrary table. Uploading Page scope user ID requires Page ID, for example, Page ID: 136400506505739
The query should complete in a few seconds. After that, check your Audience List:

You can use Scheduled Jobs with Result Export to periodically write the output result to a target destination that you specify.
Treasure Data's scheduler feature supports periodic query execution to achieve high availability.
When two specifications provide conflicting schedule specifications, the specification requesting to execute more often is followed while the other schedule specification is ignored.
For example, if the cron schedule is '0 0 1 * 1', then the 'day of month' specification and 'day of week' are discordant because the former specification requires it to run every first day of each month at midnight (00:00), while the latter specification requires it to run every Monday at midnight (00:00). The latter specification is followed.
Navigate to Data Workbench > Queries
Create a new query or select an existing query.
Next to Schedule, select None.

In the drop-down, select one of the following schedule options:

Drop-down Value Description Custom cron... Review Custom cron... details. @daily (midnight) Run once a day at midnight (00:00 am) in the specified time zone. @hourly (:00) Run every hour at 00 minutes. None No schedule.

| Cron Value | Description |
|---|---|
0 * * * * | Run once an hour. |
0 0 * * * | Run once a day at midnight. |
0 0 1 * * | Run once a month at midnight on the morning of the first day of the month. |
| "" | Create a job that has no scheduled run time. |
* * * * *
- - - - -
| | | | |
| | | | +----- day of week (0 - 6) (Sunday=0)
| | | +---------- month (1 - 12)
| | +--------------- day of month (1 - 31)
| +-------------------- hour (0 - 23)
+------------------------- min (0 - 59)The following named entries can be used:
- Day of Week: sun, mon, tue, wed, thu, fri, sat.
- Month: jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec.
A single space is required between each field. The values for each field can be composed of:
| Field Value | Example | Example Description |
|---|---|---|
| A single value, within the limits displayed above for each field. | ||
A wildcard '*' to indicate no restriction based on the field. | '0 0 1 * *' | Configures the schedule to run at midnight (00:00) on the first day of each month. |
A range '2-5', indicating the range of accepted values for the field. | '0 0 1-10 * *' | Configures the schedule to run at midnight (00:00) on the first 10 days of each month. |
A list of comma-separated values '2,3,4,5', indicating the list of accepted values for the field. | 0 0 1,11,21 * *' | Configures the schedule to run at midnight (00:00) every 1st, 11th, and 21st day of each month. |
A periodicity indicator '*/5' to express how often based on the field's valid range of values a schedule is allowed to run. | '30 */2 1 * *' | Configures the schedule to run on the 1st of every month, every 2 hours starting at 00:30. '0 0 */5 * *' configures the schedule to run at midnight (00:00) every 5 days starting on the 5th of each month. |
A comma-separated list of any of the above except the '*' wildcard is also supported '2,*/5,8-10'. | '0 0 5,*/10,25 * *' | Configures the schedule to run at midnight (00:00) every 5th, 10th, 20th, and 25th day of each month. |
- (Optional) You can delay the start time of a query by enabling the Delay execution.
Save the query with a name and run, or just run the query. Upon successful completion of the query, the query result is automatically exported to the specified destination.
Scheduled jobs that continuously fail due to configuration errors may be disabled on the system side after several notifications.
(Optional) You can delay the start time of a query by enabling the Delay execution.
After you output your user list into an Instagram Custom Audience,
Open Facebook/Instagram Ads Manager to edit the ad placement. By default, Facebook will choose to display within Instagram Ads.

Automatic Placements (default):
Edit Placement:

If you see the following error:
This ad account is not connected to Business Manager. To create or edit a customer file Custom Audience, your admin needs to connect this ad account to a business account.
You must add your Ad Account to a Business Manager account.
Follow these steps:
Create Business Manager account: https://www.facebook.com/business/help/1710077379203657
Add Ad Account: https://www.facebook.com/business/help/910137316041095

Select one of the following:
Parameter Description Add an Ad Account Adding an ad account moves the account permanently into Business Manager. You must be both the owner of the ad account and an admin in Business Manager to add the ad account. When you add an ad account and it's moved into your Business Manager, you cannot reverse the action. Also, note that all management of your ad account must be completed within your Business Manager profile. You can't add an ad account that's owned by another Business Manager. If you still want to work on an ad account that is owned by a different business, you can request access to it. Request Access to an Ad Account If you request access to an ad account in Business Manager, the admin of that Business Manager can grant you permission to work on it. Create a new Ad account If you create a new ad account in Business Manager, it'll permanently belong to that Business Manager. When the ad account is created within the Business Manager, the ad account can't be transferred to an individual owner who doesn't own a Business Manager role.
Within Treasure Workflow, you can specify the use of this data connector to export data.
Learn more at Using Workflows to Export Data with the TD Toolbelt.