To use the Xero REST API, you must first register an app in the Xero Developer Portal, obtain OAuth 2.0 credentials, and use them to authenticate requests to the API endpoints for accessing accounting data such as invoices, contacts, and bank transactions.

What You'll Need

  • A Xero organization (demo or paid)
  • Access to the Xero Developer Portal (developer.xero.com)
  • A registered application to generate Client ID and Client Secret
  • An environment to make HTTP requests (e.g., Postman, curl, or a programming language like Python or JavaScript)
  • Understanding of OAuth 2.0 Authorization Code Flow (for private or public apps) or Client Credentials Flow (for agent/app-to-app integrations)

Step-by-Step

  1. Go to developer.xero.com and sign in with your Xero account. If you don’t have a developer account, create one using your Google, GitHub, or email credentials.
  2. In the Developer Portal, navigate to "My Apps" and click "Add a new app". Choose the app type: "Private" for server-to-server access, "Public" for user-facing apps, or "Partner" if you're building an integration for distribution.
  3. Fill in the app details: name, description, and redirect URL (required for Public and Partner apps; for Private apps, this can be a placeholder like https://localhost).
  4. After creating the app, note the Client ID and Client Secret shown on the app’s overview page. These are sensitive—store them securely and never expose them in client-side code.
  5. Determine the authentication flow based on your app type:
    • For Private apps: Use the Client Credentials Flow to obtain an access token directly.
    • For Public or Partner apps: Use the Authorization Code Flow, which requires user consent via a login screen.
  6. To get an access token via Client Credentials Flow (Private app):
    • Make a POST request to https://identity.xero.com/connect/token
    • Set headers: Content-Type: application/x-www-form-urlencoded
    • Body: grant_type=client_credentials&scope=accounting.transactions%20accounting.contacts%20accounting.settings (adjust scopes as needed)
    • Authenticate using Basic Auth with your Client ID as username and Client Secret as password
    • The response will include an access_token (valid for 30 minutes) and optionally a refresh_token
  7. For Authorization Code Flow (Public/Partner apps):
    • Direct the user to: https://login.xero.com/identity/connect/authorize?response_type=code&client_id=[YOUR_CLIENT_ID]&redirect_uri=[YOUR_REDIRECT_URI]&scope=accounting.transactions%20accounting.contacts&state=[RANDOM_STRING]
    • After user consent, Xero redirects to your redirect_uri with a ?code= parameter
    • Exchange this code for tokens by POSTing to https://identity.xero.com/connect/token with:
    grant_type=authorization_code code=[THE_CODE_FROM_REDIRECT] redirect_uri=[YOUR_REDIRECT_URI] and Client ID/Secret via Basic Auth - Store the access_token and refresh_token securely
  8. Use the access token to call Xero API endpoints:
    • Set the Authorization header: Bearer [ACCESS_TOKEN]
    • Base URL: https://api.xero.com/api.xro/2.0
    • Example: To get all contacts, GET https://api.xero.com/api.xro/2.0/Contacts
    • Example: To create an invoice, POST to https://api.xero.com/api.xro/2.0/Invoices with a JSON body containing invoice details (Type, Contact, LineItems, etc.)
    • Always include the Accept: application/json header
  9. Handle token expiration:
    • Access tokens expire in 30 minutes
    • Use the refresh_token to get a new access token via POST to /connect/token with grant_type=refresh_token
    • Store new tokens securely and update your application’s stored credentials
  10. Test your integration using a Xero demo company:
    • In the Developer Portal, under your organization, enable access to a demo company
    • Use the demo company’s tenant ID in the Xero-tenant-id header when making API requests
    • Find your tenant ID by calling GET https://api.xero.com/connections and selecting the tenantId from the demo company connection

Common Mistakes

  • Using the wrong base URL—always use https://api.xero.com/api.xro/2.0, not the identity or login endpoints for API data calls
  • Forgetting to URL-encode scopes in the token request body—spaces must be %20, not left as spaces
  • Exposing Client Secret in frontend code (e.g., JavaScript in a browser)—this compromises your app’s security
  • Not setting the Xero-tenant-id header when accessing a specific organization, resulting in 400 or 403 errors
  • Using an expired or invalid access token without attempting to refresh it first
  • Requesting scopes that weren’t approved during app registration—always match requested scopes to those configured in the Developer Portal
  • Sending malformed JSON in POST/PUT bodies (e.g., incorrect data types, missing required fields like Contact or LineItems)

Tips

  • Use the Xero API Explorer (available in the Developer Portal) to test endpoints and see real request/response formats
  • Start with read-only endpoints (GET /Contacts, GET /Invoices) before attempting writes to avoid accidental data changes
  • Always check the HTTP status code and response body—Xero returns detailed error messages in JSON format (e.g., validation errors)
  • Use a refresh token rotation strategy: store the new refresh token returned after each refresh, as the old one becomes invalid
  • Respect rate limits: Xero allows 60 requests per minute per IP address for most endpoints—implement retry-after logic if you receive 429 responses
  • Webhooks are available for real-time updates—consider using them instead of polling for changes to invoices, contacts, or bank transactions
  • When creating invoices, set the Status to AUTHORISED to make it immediately visible and payable in the Xero UI
  • Use the Where clause in GET requests to filter results efficiently (e.g., Where=ContactID=="guid-here")

Troubleshooting

  • If you get 401 Unauthorized: Verify the access token is valid, not expired, and correctly formatted in the Authorization header (Bearer ). Try refreshing the token.
  • If you get 400 Bad Request with "invalid_grant": Check that your Client ID and Secret are correct, and that you’re using the right grant type for your app type.
  • If you get 403 Forbidden: Ensure the access token has the required scopes and that the Xero-tenant-id header is set to a valid tenant ID from the /connections endpoint.
  • If you get 429 Too Many Requests: Wait and retry after the time specified in the Retry-After header, or implement exponential backoff.
  • If timestamps are incorrect: Xero expects UTC timestamps in ISO 8601 format (e.g., 2024-05-20T10:30:00Z). Local times without timezone offset will cause errors.
  • If you see "The tenant is not associated with the organisation": Double-check that you’re using the correct tenant ID for the organization you intend to access.
  • If OAuth consent screen shows "This app is not verified": This is expected for apps in development. For production, submit your app for verification in the Developer Portal.
  • If you’re unable to refresh tokens: Ensure you’re storing the most recent refresh token—each use invalidates the previous one.

Frequently Asked Questions

Can I use the Xero API with a free trial organization?

Yes, you can connect your app to a Xero free trial organization using the same authentication process. Demo companies in the Developer Portal are also available for testing without affecting live data.

Do I need to renew my API credentials periodically?

Client ID and Client Secret do not expire, but access tokens are short-lived (30 minutes) and must be refreshed using the refresh token. Refresh tokens can be revoked if the user disconnects the app or after prolonged inactivity.

What is the difference between a public and private app in Xero?

A private app uses Client Credentials Flow and is suited for server-to-server integrations where no user interaction is needed. A public app uses Authorization Code Flow and requires users to log in and consent via a browser screen.

Can I access payroll data via the Xero API?

Yes, if your organization has an active payroll subscription, you can access payroll endpoints (e.g., /PayrollEmployees, /PayrollItems) by including the payroll.employees scope during authentication.

Is there a sandbox or test environment for the Xero API?

Xero provides demo companies through the Developer Portal that behave like live organizations but contain sample data. These are ideal for testing and do not require a paid subscription.

How do I handle multi-currency transactions in the API?

When creating invoices or bills in a foreign currency, include the CurrencyCode field (e.g., "USD", "EUR") and optionally the ExchangeRate. Xero will calculate amounts in the base currency using the provided rate.