Meta Ads Setup
Set up a Meta Business App and get a long-lived system user access token for the Meta Ads API.
Meta uses a long-lived access token from a system user, rather than an OAuth flow. This token doesn't expire unless manually revoked.
Overview
You need:
- Meta Business App — a developer app with the
ads_managementpermission - System user — a non-human user attached to your Business Manager
- Access token — a long-lived token generated from the system user
- Ad account ID — the ad account you want to manage
- Page ID — your Facebook Page (required for ad creatives)
Step 1: Set up Meta Business Suite
If you don't already have a Business Manager:
- Go to business.facebook.com and create a Business Manager
- Add your ad account to the Business Manager
Step 2: Create a Meta App
- Go to Meta for Developers
- Click My Apps → Create App
- Select Other as the use case, then Business as the app type
- Fill in a name (e.g. "ads-as-code") and connect it to your Business Manager
- On the app dashboard, add the Marketing API product:
- Click Add Product → find Marketing API → click Set Up
Step 3: Create a system user
System users are non-human accounts that can authenticate without expiring tokens.
- Go to Business Settings → Users → System Users
- Click Add → name it (e.g. "ads-as-code") → role: Admin
- Click Add Assets → select your ad account → grant Full Control
- If you need to manage Page-linked ads: add your Facebook Page as an asset too
Step 4: Generate an access token
- In Business Settings → System Users, click on your system user
- Click Generate New Token
- Select your app (the one you created in Step 2)
- Select these permissions:
ads_managementads_readbusiness_managementpages_read_engagement(if managing page-linked ads)
- Click Generate Token and copy it
This is a long-lived token — it doesn't expire unless revoked.
Keep this token secret. Treat it like a password. Don't commit it to git.
Step 5: Find your account ID and page ID
Ad account ID:
- Go to Ads Manager
- The account ID appears in the URL:
act=1234567890 - Use the numeric ID (with or without the
act_prefix — the SDK handles both)
Page ID:
- Go to your Facebook Page
- Click About → scroll to the bottom
- The Page ID appears under More Info
Alternatively, in Ads Manager: Settings → Page Settings → find the page ID.
Step 6: Configure
Set the access token as an environment variable:
export FB_ADS_ACCESS_TOKEN="your-long-lived-token"Add your account and page IDs to ads.config.ts:
import { defineConfig } from '@upspawn/ads'
export default defineConfig({
meta: {
accountId: '1234567890',
pageId: '9876543210',
},
})Step 7: Verify
ads doctorThis checks that your credentials are valid and the account is accessible.
Troubleshooting
OAuthException: Invalid OAuth access token
The token is invalid or has been revoked. Generate a new token from the system user in Business Settings.
OAuthException: (#200) The user hasn't authorized the application to perform this action
The token is missing required permissions. Regenerate it with ads_management, ads_read, and business_management.
Invalid account ID
Make sure you're using the numeric ID only. Strip any act_ prefix, or leave it — the SDK handles both formats.
Token expired System user tokens don't expire by default, but they can be revoked. If a token stops working, generate a new one.
App in development mode If your Meta app is in development mode, the token only works for users who have the app role (admin/developer). Switch the app to Live mode in the app dashboard, or add your system user as a tester.