Payments and finance

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

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.

Xero key types

TypePrefixSecret?ScopeExpiry
Client ID
Assigned when you create the app in the developer portal.
—NoIdentifies your OAuth 2.0 app—
Client secret
Xero says the client secret is private and should not be shared.
—YesAuthenticates your app to the token and revocation endpoints—
Access token
A JSON Web Token sent as Authorization: Bearer together with a xero-tenant-id header.
—YesThe scopes the user consented to, for the tenants they connected30 minutes
Refresh token
Only returned when the offline_access scope is requested. Each refresh returns a new refresh token.
—YesObtains new access and refresh tokens without user interaction60 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.

Open Xero's key settings

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.

If your app does not receive or save the refreshed tokens, Xero lets you retry with the previous refresh token for a 30-minute grace period. After that the old refresh token expires and the user must authorize again.

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 your Xero 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 and CLI

Xero's SDKs and docs read the key from XERO_CLIENT_ID, XERO_CLIENT_SECRET or XERO_CLIENT_BEARER_TOKEN.

Things that catch people out

  • 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.

Common Xero authentication errors

invalid_client

The client_id or client_secret sent to the token endpoint is wrong.

invalid_grant

The authorization code is incorrect, expired or already used, or the wrong grant type was sent (for example authorization_code instead of refresh_token).

AuthenticationUnsuccessful

The xero-tenant-id header is missing, wrong or not authorized for this access token. The user may have revoked access, lost permission, or the organisation is no longer active.

Using your Xero key with AI agents

A coding agent that can read your .env file or your shell history can read the key, and anything in its context can end up in logs, transcripts or a prompt-injected request. The safer pattern is to keep the key in a credential broker that performs the authenticated step for the agent and hands back only the result.

BotPassword works this way. For a Xero key, these intents fit:

IntentWhat BotPassword does
run_withRuns 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_requestMakes 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.
rotatePartly 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_secretSaves a value the agent already holds as a new vault item.

Xero also publishes its own MCP server. Whatever server the agent uses, keep the credential it authenticates with out of the agent's context.

Set it up in your agent: Aider · Amp · Claude Code · Claude Desktop · Cline · OpenAI Codex CLI · all agents

Frequently asked questions

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.

Stop pasting Xero keys into agents

BotPassword is in early access. Request an invite and we'll email you when your spot opens.