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
- Visit the Monzo Developer Portal
- Sign in with your Monzo account
- Click “Create New Application”
- 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:
Authentication Flow
Command Line Authentication
The simplest way to authenticate is using the built-in CLI tool:- Start a local web server on port 8080
- Open your default browser to the Monzo authorization page
- Handle the OAuth2 callback automatically
- 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:- macOS
- Windows
- Linux
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
Environment Variables
Environment Variables
Always store credentials in environment variables or secure configuration files, never in source code.
Token Storage
Token Storage
Monzoh stores tokens in your system’s secure cache directory. For production applications, consider using a secure token store.
HTTPS Redirect URIs
HTTPS Redirect URIs
Always use HTTPS redirect URIs in production to prevent token interception.
State Parameter
State Parameter
Use the state parameter to prevent CSRF attacks in web applications.
Troubleshooting
Authentication Failed
Authentication Failed
Error:
MonzoAuthenticationError: Invalid access token
Solutions:- Run
monzoh-auth
to re-authenticate - Check that your OAuth2 credentials are correct
- Verify that your access token hasn’t been revoked
Invalid Redirect URI
Invalid Redirect URI
Error:
invalid_request: redirect_uri mismatch
Solutions:- 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
Port Already in Use
Port Already in Use
Error:
Address already in use: 8080
Solutions:- 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
Token File Not Found
Token File Not Found
Error:
FileNotFoundError: tokens.json not found
Solutions:- Run
monzoh-auth
to create initial tokens - Check file permissions on the cache directory
- Manually specify token location if needed