OAuth 2.1 Quickstart
Alpha Feature: OAuth 2.1 integration is currently in alpha testing. It’s functional and secure, but API/UI may change. Not recommended for critical production integrations yet. Learn more about alpha status
Overview
OAuth 2.1 allows your application to:- Access the AI Stats API gateway on behalf of users
- Make requests using user’s team credits and limits
- Let users authorize/revoke access at any time
Step 1: Register Your OAuth App
First, create an OAuth application in the AI Stats dashboard.Via Dashboard
- Go to Settings → OAuth Apps
- Click “Create OAuth App”
- Fill in the details:
- Name: Your application name
- Redirect URIs:
http://localhost:3000/auth/callback(for dev) - Homepage URL: Your website (optional)
- Click “Create App”
client_id and client_secret immediately. The secret is only shown once!
Via API
Step 2: Implement PKCE
OAuth 2.1 requires PKCE (Proof Key for Code Exchange) for security.Generate Code Verifier & Challenge
Browser-Compatible Version
Step 3: Redirect User to Authorization
Build the authorization URL and redirect the user:Available Scopes
| Scope | Description |
|---|---|
openid | Required for OAuth 2.1 |
email | Access user’s email address |
profile | Access user’s profile information |
gateway:access | Make API requests on user’s behalf |
Step 4: Handle the Callback
After the user authorizes, they’ll be redirected back with acode:
Step 5: Exchange Code for Tokens
Exchange the authorization code for access and refresh tokens:refresh_token secure. Never expose it to the browser.
Step 6: Make API Requests
Use the access token to make requests to the AI Stats API:Step 7: Refresh Access Tokens
Access tokens expire after 1 hour. Use the refresh token to get a new one:Complete Example (Next.js)
Here’s a complete working example:Testing Your Integration
1. Test Authorization Flow
2. Test API Request
Security Best Practices
✅ Do’s
- Use PKCE - Always required for OAuth 2.1
- Validate state - Prevent CSRF attacks
- Use HTTPS - Required in production (localhost HTTP is OK for dev)
- Store secrets server-side - Never expose client_secret in browser
- Refresh tokens proactively - Don’t wait for 401 errors
- Handle revocation gracefully - Check for 401 and prompt re-authorization
❌ Don’ts
- Don’t use implicit flow - Not supported (OAuth 2.1 requirement)
- Don’t store tokens in localStorage - Use httpOnly cookies
- Don’t log tokens - Treat them like passwords
- Don’t share client_secret - Keep it secret, keep it safe
- Don’t skip state validation - Critical for CSRF protection
Troubleshooting
”Invalid redirect_uri”
Cause: The redirect URI doesn’t match what’s registered in your OAuth app. Fix: Ensure the URI matches exactly (including protocol, port, and path).”Invalid code_challenge”
Cause: PKCE verification failed. Fix: Ensure you’re using the samecode_verifier from step 2 when exchanging the code.
”Authorization has been revoked”
Cause: User revoked access from their settings. Fix: Redirect user to re-authorize your app.”Token has expired”
Cause: Access token expired (1 hour lifetime). Fix: Use the refresh token to get a new access token.Next Steps
- API Reference - Detailed OAuth endpoint documentation
- Token Refresh Guide - Handling token expiration
- Security Best Practices - Production security checklist
- Example Apps - Complete working examples
Need Help?
- Discord: Join our community
- Email: support@aistats.ai
- GitHub Issues: Report a bug