Overview
Courier Inbox and Toast require a user-scoped JWT to authenticate requests from your frontend. Your backend must generate these tokens server-side using your Courier API key, then pass them securely to your client application. If you’re adding Inbox or Toast to an existing application, you can implement JWT generation in your backend with minimal changes to your existing authentication flow.JWT Authentication Benefits
- The token generated by your backend contains only the necessary permissions for Inbox and Toast to function, which gives you fine-grained control over user access.
- Because tokens are scoped to specific users, each user can only access their own messages and preferences.
- Tokens expire after a set duration, limiting the impact of potential security leaks.
- Your Courier API key stays secure on your backend and is never exposed to client-side code.
Required Permissions
When generating a JWT, you must include these scopes:user_id:{{userId}}- Grants access to a specific userinbox:read:messages- Allows reading inbox messagesinbox:write:events- Enables marking messages as read, archived, etc.read:preferences- Allows reading user notification preferenceswrite:preferences- Enables updating user preferencesread:brands- Grants access to brand settings
read:user-tokens- For managing push tokenswrite:user-tokens- For registering device tokens
Implement JWT Generation
Follow these steps to add JWT generation to your backend:1
Install dependencies
Install the required packages for your backend framework.
2
Store your Courier API key
Store your Courier API key as an environment variable (never hardcode it in your source code). You can find your keys in Courier settings.
3
Create the JWT endpoint
Create a POST endpoint that accepts a user ID and calls Courier’s token generation API.
The endpoint validates the user ID, calls Courier’s
/auth/issue-token endpoint with the required scopes, and returns the JWT token. Token expiration is set to 1 day (expires_in: '1d').4
Add user validation (recommended)
Add validation to ensure the requesting user is authenticated and authorized.
It’s best practice to verify that the user you’re issuing a JWT for is the same user as the logged-in session or authenticated identity in your application, according to your security requirements.
5
Start your server
Start your backend server and verify the endpoint is accessible.Node.js:Python:
Pass the Token to Your Frontend
After your backend generates the JWT, you will be able to retrieve it to enable Courier Inbox or Toast features in your front-end project.1. Call Your Backend Endpoint
2. Authenticate Courier SDK
Pass the token to Courier’s authentication method:Best Practice: Generate a new JWT when the user logs in, and refresh it before expiration if your token lifetime exceeds the user session duration.
Security Best Practices
Token Expiration
Set appropriate expiration times based on your security requirements:- Short sessions:
'1h'or'30m'for high-security applications - Standard sessions:
'1d'for typical web applications - Long sessions:
'7d'or'30d'only if you have robust token refresh mechanisms
API Key Protection
- Store your Courier API key in environment variables, never in source code
- Use different API keys for development, staging, and production
- Rotate API keys periodically
- Restrict API key permissions in the Courier dashboard when possible
HTTPS Only
Always use HTTPS in production to encrypt JWT tokens in transit. Never send tokens over unencrypted HTTP connections.Debugging Tips
- Log token generation: Log successful token generation (but never log the full token value)
- Check scopes: Verify the token includes all required scopes for your use case
- Monitor expiration: Track when tokens expire to identify refresh issues
- Test locally: Use Courier’s test API key to verify endpoint behavior before deploying
Learn More: For complete API documentation, see the Auth API Reference and Issue Token endpoint.