# Meta Graph API API tokens: find, store, rotate, and use them safely with AI agents

Source: https://botpassword.com/keys/meta-graph
Last checked: 2026-11-08

Meta Graph API access tokens authorize calls to Facebook and Instagram data through `graph.facebook.com`. User tokens come from Facebook Login or the Graph API Explorer and last about one to two hours, or about 60 days once exchanged for a long-lived token; Page tokens from a long-lived user token do not expire. System user tokens from Meta Business Manager suit servers and can be set to never expire, and the app secret signs everything, so it must stay on your server.

## Key types

| Type | Prefix | Secret | Scope | Expiry |
|---|---|---|---|---|
| User access token | — | Yes | The permissions the user granted to your Meta app | Short-lived tokens last about one to two hours; long-lived tokens last about 60 days |
| Page access token | — | Yes | Read, write and modify data for one Facebook Page the user has a role on | Page tokens obtained from a long-lived user token have no expiration date |
| System user access token | — | Yes | The scopes chosen for a system user in Business Manager, such as `ads_management` or `pages_read_engagement` | Either never expires or is valid for 60 days and must be refreshed |
| App access token and app secret | — | Yes | Read and modify app settings; the app secret also signs `appsecret_proof` and token exchanges | Lasts until the app secret is reset |

## How to create a Meta Graph API key

1. Create or open your app in the Meta App Dashboard at `developers.facebook.com`.
2. Open the Graph API Explorer (`developers.facebook.com/tools/explorer`) with your app selected.
3. Click **Generate Access Token** and approve the Facebook Login dialog to get a user access token.
4. Add the permissions you need from **Add a Permission**, then click **Generate Access Token** again.
5. For a server, exchange the token on your backend for a long-lived one, or generate a system user token in Business Manager.

## Limiting what the key can do

Graph API tokens carry only the permissions the user or system user approved, so request the smallest set of permissions, such as `pages_show_list` instead of broad management permissions. Turn on **Require App Secret** under **App Settings** → **Advanced** → **Security** so every server call must include `appsecret_proof`, a SHA-256 HMAC of the access token keyed with the app secret; a stolen token alone is then useless without the secret. Time-stamped proofs expire after 5 minutes. Use the Access Token Tool to list your tokens and inspect their scopes and expiry.

## How to rotate a Meta Graph API key

1. For user tokens, exchange a valid short-lived token on your server with `grant_type=fb_exchange_token` to get a new long-lived token before the old one lapses.
2. For expiring system user tokens, refresh them before the 60-day validity ends.
3. For the app secret, reset it in the App Dashboard under **Basic** → **App Secret** and update your servers.

## How to revoke a Meta Graph API key

1. Revoke a compromised system user token with the `oauth/revoke` endpoint, sending `revoke_token`, `client_id`, `client_secret` and `access_token`.
2. Reset the app secret under **Basic** → **App Secret** if it was exposed; this also stops `{app-id}|{app-secret}` tokens.
3. Check the Access Token Tool afterwards to confirm which tokens remain and what they can do.

## If the key leaked

1. Revoke the exposed system user token with `oauth/revoke`, or have the affected user log in again for a fresh token.
2. Reset the app secret if it, or an app access token, appeared anywhere outside your server.
3. Turn on **Require App Secret** so tokens cannot be used without `appsecret_proof`.
4. Review Page and ad account activity for actions you did not take.

## Gotchas

- Subcode 460 means the user changed their password, which invalidates their tokens even inside the 60-day window.
- Subcode 492 on a Page token means the user behind the token no longer has a suitable role on the Page.
- Never put the app secret or an app access token in client code or app binaries; Meta says to use them only server to server.
- Meta's official MCP server (`mcp.facebook.com/ads`) covers ads management, not the whole Graph API.

## Using this key with AI agents (BotPassword)

- `run_with`: Runs a command with the key injected as an environment variable or a 0600 temporary file, never on the command line, and redacts the key from the output.
- `http_request`: Makes an authenticated API call after checking the destination against the hosts this key may be sent to. The response comes back with credential-shaped values redacted, and one that holds the key itself is refused.

## FAQ

### How do I get a Facebook Graph API access token?

Open the Graph API Explorer at `developers.facebook.com/tools/explorer`, select your app, click **Generate Access Token** and approve the login dialog. That gives a short-lived user token for testing.

### How long do Meta Graph API access tokens last?

Short-lived user tokens last about one to two hours and long-lived ones about 60 days. Page tokens from a long-lived user token have no expiration, and system user tokens can be set to never expire or to 60 days.

### How do I get a long-lived access token?

On your server, call `oauth/access_token` with `grant_type=fb_exchange_token`, your app ID, app secret and the short-lived token. The result lasts about 60 days.

### Why did my long-lived Meta access token stop working before 60 days?

Meta says token lifetimes can change without warning or end early. A password change (subcode 460), a security checkpoint (459) or losing a Page role (492) all invalidate tokens, so handle error 190 by getting a new token.

### Is there an official Meta MCP server for AI agents?

Meta hosts an Ads MCP Server at `mcp.facebook.com/ads` for managing ads, reporting and catalogs from an AI agent. For other Graph API work, give the agent a narrowly scoped system user token instead of a personal user token.

### Can an AI agent use my Meta Graph API key without seeing it?

Yes, if the key never enters the agent's context. Instead of pasting the key into a chat or leaving it in a `.env` file the agent can read, keep it in a credential broker that injects it into the command or request on the agent's behalf. BotPassword does this with `run_with`, `http_request`, so the agent gets the result and never the value.

## Sources

- https://developers.facebook.com/docs/facebook-login/guides/access-tokens
- https://developers.facebook.com/docs/facebook-login/guides/access-tokens/get-long-lived
- https://developers.facebook.com/docs/graph-api/get-started
- https://developers.facebook.com/docs/graph-api/guides/error-handling
- https://developers.facebook.com/docs/graph-api/securing-requests
- https://developers.facebook.com/documentation/facebook-login/security
- https://developers.facebook.com/docs/business-management-apis/system-users/install-apps-and-generate-tokens
- https://developers.facebook.com/documentation/ads-commerce/ads-ai-connectors/ads-mcp-server/ads-mcp-server-overview
