> Documentation index: https://docs.zernio.com/llms.txt. Every page is also Markdown: add .md to its URL.

# Connect

Connect a Meta Ads account with the Zernio API, every step with code.

## Choose a login mode
Call `GET /v1/connect/facebook/ads` with `profileId`. Meta has two login options:

- **Classic**, the default (`loginMode=classic`), uses the connected Facebook posting account and its token. An active parent with `ads_management` and `ads_read` can be reused without another OAuth round trip.
- **Facebook Login for Business** (`loginMode=business`, also accepted on `GET /v1/connect/instagram/ads`) creates an independent `metaads` connection with a Business Integration System User token. No posting account is created or required. Open the returned `authUrl` in a browser to complete consent.

Meta needs a Page as the actor for ad creatives, so select a Page for either path when creating ads. Business login refuses a grant with no Page (`no_pages_granted`).

[Connections and billing](#connections-and-billing) explains which accounts each path creates and which ones bill.

The [connecting accounts guide](/guides/connecting-accounts#facebook-login-for-business) covers Page selection. The [classic ads-only flow](/guides/connecting-accounts#connect-meta-ads-only-skip-the-page-picker) covers headless connections and scoping sync to specific ad accounts.

### OAuth scopes

`ads_management` covers campaign management, Reach and Frequency and the Business Manager list.

[Creating an ad account](#create-an-ad-account) also requires `business_management` and business admin access.

By default the classic consent also asks for the Facebook posting, comments, messaging and Page insights permissions. Pass `scopes=ads_only` on `GET /v1/connect/facebook/ads` to ask for the ads permissions alone:

- Asked: `pages_show_list` and `pages_read_engagement` (the Page picker), `business_management`, `pages_manage_metadata` (Lead Ads webhooks), `ads_management`, `ads_read`, `leads_retrieval`, `pages_manage_ads` and `instagram_basic`.
- Left out: `pages_manage_posts`, `pages_manage_engagement`, `pages_read_user_content`, `pages_messaging`, `page_events` and `read_insights`.
- Ad accounts, campaigns, audiences, lead forms and Instagram placements work as with the default consent.
- When the profile's Facebook account posts or holds more than ads and the connect must sign in again (`force=true`, or a token that needs a reconnect), the request answers `409` with `code: "invalid_resource_state"`. Connect with `scopes=ads` instead: without `force`, it reuses that account's token when the token already holds the ads permissions.
- It is available on the classic Facebook login only. Instagram ads land on the same `metaads` connection.

Accounts connected before ads were enabled on the team do not carry these scopes. Reconnect them, and read [Account health](/accounts/get-all-accounts-health) to see what a connected account was granted.

The general scope rule is on the [connecting accounts guide](/guides/connecting-accounts#scopes).

### Find your ad account id

Ad-account operations use two ids:

- `accountId`: the connected posting or Meta Ads account
- `adAccountId`: the Meta ad account

Call `GET /v1/ads/accounts` to list the ad accounts the connection can reach. On a connection with an ad-account selection it lists only the selected ones unless you pass `live=true`.

Creating a new ad account instead takes an active `metaads` account as `accountId` and the owning `businessId`.

<Tabs items={['Node.js', 'Python', 'curl']}>
<Tab value="Node.js">
```typescript
import Zernio from '@zernio/node';

const zernio = new Zernio();

const { data: adAccounts } = await zernio.adaccounts.listAdAccounts({
  query: { accountId: '66b2e19d8c3f5a7e9d0b1c2d' }
});

const adAccountId = adAccounts.accounts[0].id;
```
</Tab>
<Tab value="Python">
```python
from zernio import Zernio

client = Zernio()

ad_accounts = client.ad_accounts.list_ad_accounts(
    account_id="66b2e19d8c3f5a7e9d0b1c2d"
)

ad_account_id = ad_accounts["accounts"][0]["id"]
```
</Tab>
<Tab value="curl">
```bash
curl "https://zernio.com/api/v1/ads/accounts?accountId=66b2e19d8c3f5a7e9d0b1c2d" \
  -H "Authorization: Bearer $ZERNIO_API_KEY"
```
</Tab>
</Tabs>

Response (`200`):

```json
{
  "accounts": [
    {
      "id": "act_1234567890",
      "name": "Acme",
      "currency": "USD",
      "accountStatus": 1,
      "timezoneName": "America/New_York",
      "minimumDailyBudget": 1,
      "selectable": true,
      "unusableReason": null
    }
  ]
}
```

`accounts[].id` is the `adAccountId` for every other call.

Budgets and bids are whole units of `currency` (`75` is $75.00 on a USD account), never cents.

### Create an ad account

[`POST /v1/ads/accounts`](/ad-accounts/create-ad-account) creates a Meta ad account in the customer's **own business portfolio**.

Discover portfolios with [`GET /v1/ads/businesses`](/ad-accounts/list-meta-businesses). Then send `accountId` (an active `metaads` connection), `businessId`, `name`, `currency` and `timezoneId`.

The connection needs `business_management` and business admin access.

System-user tokens can return an empty businesses list. Supply the known business ID in that case.

`timezoneId` is Meta's **numeric timezone ID**, not an IANA name. The reference links Meta's timezone list. Use the returned `adAccountId` with the existing ads endpoints.

<Callout type="warn">
The new account has no payment method. The customer must add one in Ads Manager before ads deliver; Zernio cannot add it through the API.
</Callout>

Meta caps how many ad accounts a business can create and may require business verification. Closing an account does not guarantee more capacity, and an ad account can never truly be deleted once created.

This call is **not idempotent** and Zernio never automatically retries it.

After a timeout or a `502` with `details.creationStatus: "unknown"`, check the business in Ads Manager before creating again.

A `201` with `connectionUpdated: false` still means the account exists. Follow the returned recovery instructions to reconnect with its ID and the previous scoped IDs, instead of repeating the create.

## Connections and billing

A Meta Ads connection is its own account, with `platform: "metaads"` and its own `accountId`.

What else exists next to it depends on how it was connected:

| How it was connected | Accounts created | Where its Pages live |
|---|---|---|
| Business login (`loginMode=business`) | One `metaads` account with `parentAccountId: null`. No Facebook account is created. | In the `metaads` account. `GET /v1/accounts/{accountId}/facebook-page` lists the granted Pages and `selectedPageId` (add `refresh=true` after granting more); `PUT` with `selectedPageId` changes the default Page. |
| Classic, from an existing Facebook account | One `metaads` account whose `parentAccountId` is that Facebook account. If the parent already has `ads_management` and `ads_read`, no OAuth runs. Pass `accountId` when the profile has several Facebook accounts. | In the parent Facebook account. |
| Classic, through OAuth with no Facebook account for the selected Page | The `metaads` account, plus a Facebook account for that Page with `enabled: false`. | In that hidden Facebook account. |

**The hidden Facebook account** is not shown in the dashboard, cannot be posted to (`ACCOUNT_NOT_ENABLED_FOR_POSTING`) and is not billed. `GET /v1/accounts` returns it with `enabled: false`.

Connecting the same Page later through `GET /v1/connect/facebook`, as the same Facebook user, turns that row into a normal Facebook account. It keeps the same `accountId`, and from then on it bills.

**A `metaads` account is for ads only.** It cannot serve organic Facebook or Instagram Insights.

The [Page insights](/analytics/get-facebook-page-insights) and [Instagram account insights](/analytics/get-instagram-account-insights) endpoints answer `400 INVALID_FIELD_VALUE` for its `accountId`.

For posting, the inbox and organic Insights on the same Page, connect it with `GET /v1/connect/facebook` and select-page (or connect the Instagram account).

That creates a separate `facebook` account with its own `accountId`, billed on its own.

Billing counts every active account once across your team, `metaads` included:

- One `metaads` account counts once, however many ad accounts it reaches. Running ads also meter separately per active ad ([Ads pricing](/pricing/ads#managed-ads)).
- A Facebook Page and an Instagram account connected for posting count once each. A business-login `metaads` account plus the same Page connected organically is 2 accounts.
- Hidden (`enabled: false`) and disconnected accounts do not count.
- The first 2 accounts are free.
- Connecting another without a card on file answers `402` with `code: "PAYMENT_REQUIRED"` and `reason: "free_tier_exceeded"`. On an OAuth redirect it arrives as `error=payment_required`. See [Payment method required](/pricing/payment-method-required).

[Pricing](/pricing/social#what-counts-as-a-connected-account) has the rates.

### Sync ads through a Facebook account

A Facebook account granted `ads_management` and `ads_read` can sync the Meta ads of the ad accounts it reaches, without a `metaads` connection. The setting is off by default.

Turn it on with `PUT /v1/accounts/{accountId}` and `{ "metaAdsSyncEnabled": true }`, or with the Ads sync switch on the Facebook account in the dashboard. `GET /v1/accounts` returns `metaAdsSyncEnabled` on Facebook accounts.

The first sync starts at once, like a new Meta Ads connection: 14 days first, then 90 days of history in the background.

It fires `account.ads.initial_sync_completed` for the Facebook `accountId` with `platform: "metaads"`. It fires again on every switch from off to on.

- Synced active ads count on the managed ads meter like ads from a Meta Ads connection ([Ads pricing](/pricing/ads)).
- Without the two permissions the call answers `403` with `code: "reconnect_required"`. Reconnect the Facebook account and grant them.
- A team without ads access answers `403` with `code: "ads_addon_required"`, and a team past its managed ads allowance answers `403` with `code: "ads_allowance_exceeded"`.
- Any other platform answers `400` with `code: "invalid_field_value"`.
- While a `metaads` connection has this Facebook account as `parentAccountId`, that connection syncs the ads instead.
- Turning it off stops the sync. Ads already synced are kept, stop updating and stop counting on the meter. A Meta Ads connection that reaches the same ad account takes them over.
- Posting, the inbox and comments on the Facebook account do not change.

### A Page is missing

The Page lists show exactly the Pages Meta's token grants. That covers `GET /v1/connect/facebook/select-page` and the Pages stored on a `metaads` account.

Zernio does not filter out Pages already connected elsewhere. A missing Page was not ticked on Meta's Page step.

Meta pre-fills the previous selection. Ask the user to connect again, click "Edit previous settings" and tick the Page.

When no Page is granted at all, the redirect carries `error=no_facebook_pages` and an `error_reason`:

- `pages_permission_declined`
- `no_pages_granted`
- `granted_pages_not_listed`

Each is described in [If it fails](/guides/connecting-accounts#if-it-fails).

### Headless connections

In the classic headless flow, the `connect_token` on the redirect is valid for 15 minutes. Send it as `X-Connect-Token` to the list and select Page calls.

Business login has no headless Page step and does not accept `X-Connect-Token` for one. Pass `pageId` on `GET /v1/connect/facebook/ads`, or change the default Page afterwards with `PUT /v1/accounts/{accountId}/facebook-page`.