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

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

Xero API access uses OAuth 2.0: an app created in the Xero developer portal gets a client ID, you generate a client secret, and the authorization code flow returns an access token that lasts 30 minutes and a refresh token that lasts 60 days. Xero refresh tokens rotate on every use, and each API call also needs a `xero-tenant-id` header naming the connected organisation.

## Key types

| Type | Prefix | Secret | Scope | Expiry |
|---|---|---|---|---|
| Client ID | — | No | Identifies your OAuth 2.0 app | — |
| Client secret | — | Yes | Authenticates your app to the token and revocation endpoints | — |
| Access token | — | Yes | The scopes the user consented to, for the tenants they connected | 30 minutes |
| Refresh token | — | Yes | Obtains new access and refresh tokens without user interaction | 60 days |

## How to create a Xero key

1. Sign in to the Xero developer portal and open **My Apps**.
2. Create an OAuth 2.0 app and select the **Auth Code** grant type (use PKCE instead for desktop or mobile apps).
3. Copy the client ID, then generate a client secret and store it in a secrets vault.
4. Send the user to `https://login.xero.com/identity/connect/authorize` with the scopes you need, including `offline_access` if you want a refresh token.
5. Exchange the returned code at `https://identity.xero.com/connect/token`, then call `GET https://api.xero.com/connections` to find the tenant IDs the user connected.

## Limiting what the key can do

Xero scopes are additive: request the minimum for what the user is doing now and add more later by sending the user through the flow again. Scopes cannot be removed from an existing token; the only way to reduce consented scopes is to revoke the token and start again. Broad Accounting API scopes such as `accounting.transactions` are being replaced by granular scopes such as `accounting.invoices` and `accounting.payments`, and broad scopes remain available until September 2027.

## How to rotate a Xero key

1. POST `grant_type=refresh_token` and the current refresh token to `https://identity.xero.com/connect/token`, with Basic auth made from the client ID and client secret.
2. Save both the new access token and the new refresh token from the response.
3. To replace the client secret, generate a new secret for the app in **My Apps** and deploy it to every service that calls the token endpoint.

## How to revoke a Xero key

1. POST `token=` with the refresh token to `https://identity.xero.com/connect/revocation`, using Basic auth with the client ID and client secret. This revokes the refresh token and removes all of that user's connections to your app.
2. To disconnect one organisation, send `DELETE https://api.xero.com/connections/{connectionId}` with a valid access token.
3. Or sign in to the Xero developer portal, select the app, open **Connections management**, click the three dots next to the tenant and choose **Disconnect**.

## If the key leaked

1. Revoke exposed refresh tokens at the revocation endpoint, which also removes the user's connections to your app.
2. If the client secret leaked, generate a new client secret in **My Apps** and redeploy every service that uses it.
3. Review **Connections management** in the developer portal for tenants you do not recognise and disconnect them.
4. Ask affected users to reconnect through your **Connect to Xero** flow.

## Environment variables

- `XERO_CLIENT_ID`
- `XERO_CLIENT_SECRET`
- `XERO_CLIENT_BEARER_TOKEN`

## Gotchas

- Refresh tokens rotate on every use. Store the new refresh token each time, or the connection is lost after the 30-minute grace period.
- A refresh token is only issued if you request the `offline_access` scope.
- Every API call needs the `xero-tenant-id` header as well as the bearer token, and one token can reach several tenants.
- Redirect URIs must be HTTPS. For testing Xero allows `http://localhost/`, but `http://127.0.0.1` cannot be used.

## 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.
- `rotate`: Partly built: creates a new key, stores it as pending, verifies it, promotes it, then revokes the old key after a grace period. Today it works for Cloudflare tokens and AWS IAM keys you have set up, and an agent can start it only under a grant with autopilot; other providers cannot be rotated through their API yet.
- `store_secret`: Saves a value the agent already holds as a new vault item.

## FAQ

### How long do Xero access and refresh tokens last?

A Xero access token lasts 30 minutes and a refresh token lasts 60 days. The ID token lasts 5 minutes.

### Where do I get a Xero client secret?

In the Xero developer portal under **My Apps**. Open the app and generate a client secret; the client ID is assigned when you create the app.

### Why does my Xero refresh token stop working?

Xero refresh tokens are single use. Each refresh returns a new one, and the old one only works for a further 30 minutes, so always save the newest refresh token.

### How do I disconnect a Xero organisation from my app?

Send `DELETE https://api.xero.com/connections/{connectionId}`, or use **Connections management** in the developer portal and choose **Disconnect**.

### How do I revoke a Xero refresh token?

POST the token to `https://identity.xero.com/connect/revocation` with Basic auth. A successful revocation returns HTTP 200 and removes the user's connections.

### Can an AI agent use my Xero 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`, `store_secret`, so the agent gets the result and never the value.

## Sources

- https://developer.xero.com/documentation/guides/oauth2/auth-flow/
- https://developer.xero.com/documentation/guides/oauth2/scopes/
- https://developer.xero.com/documentation/guides/oauth2/troubleshooting/
- https://github.com/XeroAPI/xero-mcp-server
