Get Expense Data from Xero API Using Python

Introduction

This article is part of our in-depth series on the Xero API and focuses specifically on retrieving expense data using the API.

If you're building integrations around finance automation, expense reconciliation, or reporting workflows, accessing expense claims programmatically is a core use case.

You can explore the complete Acconting API guide, including authentication, rate limits, and other use cases, here.

This guide walks through the prerequisites, authentication setup, API endpoint usage, and implementation steps required to fetch expense data securely and reliably.

Pre-requisites

Before calling the API, ensure the following:

  • Access to a Xero account with API permissions
  • OAuth 2.0 authentication setup for secure API access
  • Python environment with required libraries installed (requests, json, requests_oauthlib)

Without proper OAuth configuration and permissions, the API call will fail. Set this up correctly before moving ahead.

API Endpoint

Expense Claims Endpoint

GET https://api.xero.com/api.xro/2.0/ExpenseClaims

This endpoint retrieves expense claims data from Xero.

Step-by-Step Process

1. Set Up OAuth 2.0 Authentication

import requests, json
from requests_oauthlib import OAuth2Session

# Define your client ID, client secret, and redirect URI
client_id = 'YOUR_CLIENT_ID'
client_secret = 'YOUR_CLIENT_SECRET'
redirect_uri = 'YOUR_REDIRECT_URI'

# Create an OAuth2 session
oauth = OAuth2Session(client_id, redirect_uri=redirect_uri)

# Get the authorization URL
authorization_url, state = oauth.authorization_url(
    'https://login.xero.com/identity/connect/authorize'
)

print('Please go to %s and authorize access.' % authorization_url)

This step generates the authorization URL where the user grants access to your application.

2. Retrieve Access Token

# After authorization, you'll get a response URL
response_url = 'RESPONSE_URL_FROM_AUTHORIZATION'

# Fetch the token
token = oauth.fetch_token(
    'https://identity.xero.com/connect/token',
    client_secret=client_secret,
    authorization_response=response_url
)

Once authorized, exchange the authorization code for an access token.

3. Fetch Expense Data

# Define the endpoint
url = 'https://api.xero.com/api.xro/2.0/ExpenseClaims'

# Make the request
response = requests.get(
    url,
    headers={'Authorization': 'Bearer ' + token['access_token']}
)

# Parse the response
expense_data = response.json()

print(json.dumps(expense_data, indent=2))

This call retrieves expense claims data in JSON format.

Common Pitfalls

Most integration failures happen due to preventable configuration mistakes. Watch out for:

  1. Incorrect OAuth setup leading to authentication failures
  2. Expired access tokens causing unauthorized errors
  3. API rate limits being exceeded
  4. Incorrect endpoint URLs resulting in 404 errors
  5. Insufficient permissions for accessing expense data
  6. Misconfigured redirect URIs causing OAuth errors
  7. Lack of proper error handling for failed API responses

Production-grade integrations must include token refresh logic, structured error handling, and rate limit management.

Frequently Asked Questions

1. How do I refresh an expired token?

Use the refresh token provided during the initial token exchange to request a new access token.

2. What is the rate limit for Xero API?

Refer to Xero’s official API documentation for current rate limit details.

3. Can I access historical expense data?

Yes. You can specify date ranges in your API requests to retrieve historical records.

4. Is there a sandbox environment for testing?

Yes. Xero provides a demo company that can be used for testing API integrations.

5. How should I handle API errors?

Implement structured error handling to process HTTP status codes and response payloads gracefully.

6. Can I filter expense claims by status?

Yes. Query parameters can be used to filter results.

7. What data formats are supported?

The API uses JSON for both requests and responses.

Knit for Xero API Integration

For faster and more streamlined access to the Xero API, Knit provides a unified integration layer.

With a single integration, Knit manages authentication, authorization, and ongoing maintenance. This reduces engineering overhead and simplifies long-term API management, while ensuring a reliable connection to the Xero API.

If you're building finance automation workflows, expense reconciliation systems, or unified accounting integrations, this approach ensures secure, scalable access to Xero expense data.

#1 in Ease of Integrations

Trusted by businesses to streamline and simplify integrations seamlessly with GetKnit.