Skip to main content

Overview

Monzoh uses OAuth2 for authentication with the Monzo API. This guide walks you through setting up OAuth2 credentials and completing the authentication flow.

Prerequisites

  • A Monzo account (personal or business)
  • A registered OAuth2 application in the Monzo Developer Portal

Setting Up OAuth2

1. Create a Developer Application

  1. Visit the Monzo Developer Portal
  2. Sign in with your Monzo account
  3. Click “Create New Application”
  4. Fill in your application details:
    • Name: Your application name
    • Description: Brief description of your app
    • Redirect URI: http://localhost:8080/callback (for local development)
    • Logo: Optional application logo
For production applications, use a proper HTTPS redirect URI pointing to your application’s callback endpoint.

2. Configure Environment Variables

Create a .env file in your project root with your OAuth2 credentials:
Alternatively, you can set these as environment variables:

Authentication Flow

Command Line Authentication

The simplest way to authenticate is using the built-in CLI tool:
This command will:
  1. Start a local web server on port 8080
  2. Open your default browser to the Monzo authorization page
  3. Handle the OAuth2 callback automatically
  4. Save your access and refresh tokens securely
Tokens are saved in your system’s cache directory:
  • macOS: ~/Library/Caches/monzoh/tokens.json
  • Windows: %LOCALAPPDATA%/monzoh/tokens.json
  • Linux: ~/.cache/monzoh/tokens.json

Programmatic Authentication

For more control, you can handle the OAuth2 flow programmatically:

Custom Redirect URI

If you’re building a web application, you can use a custom redirect URI:

Token Management

Automatic Token Refresh

Monzoh automatically refreshes expired access tokens using the stored refresh token:

Manual Token Management

You can also manage tokens manually:

Token Storage Locations

Tokens are stored in different locations based on your operating system:

Client Initialization

Default Initialization

The simplest way to create a client is using stored tokens:

Custom Access Token

You can provide an access token directly:

Custom HTTP Client

For advanced use cases, you can provide a custom HTTP client:

Testing Authentication

Check Authentication Status

Validate Token Permissions

Security Best Practices

Always store credentials in environment variables or secure configuration files, never in source code.
Monzoh stores tokens in your system’s secure cache directory. For production applications, consider using a secure token store.
Always use HTTPS redirect URIs in production to prevent token interception.
Use the state parameter to prevent CSRF attacks in web applications.

Troubleshooting

Error: MonzoAuthenticationError: Invalid access tokenSolutions:
  • Run monzoh-auth to re-authenticate
  • Check that your OAuth2 credentials are correct
  • Verify that your access token hasn’t been revoked
Error: invalid_request: redirect_uri mismatchSolutions:
  • Ensure the redirect URI matches exactly with your OAuth2 app settings
  • Check for trailing slashes or protocol mismatches
  • Update your app settings in the Monzo Developer Portal
Error: Address already in use: 8080Solutions:
  • Use a different port: monzoh-auth --port 8081
  • Kill any processes using port 8080
  • Update your OAuth2 app’s redirect URI to match the new port
Error: FileNotFoundError: tokens.json not foundSolutions:
  • Run monzoh-auth to create initial tokens
  • Check file permissions on the cache directory
  • Manually specify token location if needed

Next Steps

Now that you’re authenticated, you can start making API calls:

Accounts

List accounts and get balance information

Transactions

Retrieve and manage transaction data

Error Handling

Handle authentication and API errors

Mock Mode

Test without real API calls