NanoGPT CLI Device Login Integration Guide
This guide explains how to integrate device login into your CLI application so users can authenticate without embedding a browser.Overview
The NanoGPT CLI device login flow works like the GitHub CLI or Claude Code login:- Your CLI requests a login code from NanoGPT.
- User opens a URL in their browser and signs in.
- User approves the CLI access.
- Your CLI receives an API key (
sk-nano-...).
Step 1: Start the login flow
Make a POST request to initiate login:Request body
| Field | Type | Required | Description |
|---|---|---|---|
client_name | string | No | Your application name (max 64 chars). Used to identify the API key in the user’s account. |
Response
| Field | Description |
|---|---|
device_code | Secret token for polling. Keep this secure and do not display to the user. |
user_code | Human-readable code displayed on the approval page. |
verification_uri | Base URL for the user to visit. |
verification_uri_complete | Full URL with the code pre-filled. Display this to the user. |
expires_in | Seconds until the code expires (600 = 10 minutes). |
interval | Recommended polling interval in seconds. |
Step 2: Direct the user to approve
Display theverification_uri_complete URL to the user. They should open it in their browser.
Example output in your CLI:
- They sign in with their NanoGPT account (Google, Discord, etc.).
- They see the verification code and click “Approve”.
- They can close the browser and return to the CLI.
Step 3: Poll for approval
Poll the status endpoint until you receive the API key:Response codes
| Status | Response | Action |
|---|---|---|
202 | {"status": "authorization_pending"} | Continue polling |
200 | {"status": "approved", "key": "sk-nano-..."} | Success. Store the key |
410 | {"status": "expired"} | Code expired. Restart the flow |
409 | {"status": "consumed"} | Key already delivered. Check your storage |
404 | {"error": "invalid_code"} | Invalid device_code |
Polling example (bash)
Step 4: Use the API key
Once you have the API key, use it for all NanoGPT API requests. List available models:Complete integration example
API key management
- API keys created through this flow are named
CLI (<client_name>)in the user’s account. - Users can view and revoke keys at https://nano-gpt.com/settings (API Keys section).
- If the same
client_nameis used multiple times, the existing key is reused. - Keys do not expire unless manually revoked.
Error handling
| Scenario | How to handle |
|---|---|
| User does not approve in time | Codes expire after 10 minutes. Restart the flow. |
Invalid device_code | Check you are using the exact code from /start. |
| 401 on API requests | Key may have been revoked. Re-authenticate. |
| Rate limited | /start: 10 req/min, /poll: 60 req/min per IP. Use the recommended interval. |
Security best practices
- Store the API key securely (OS keychain, encrypted config, or secure credential storage).
- Never log or display the
device_code. - Handle key revocation by prompting the user to re-authenticate on 401s.
- Use HTTPS only (
https://nano-gpt.com).
Rate limits
| Endpoint | Limit |
|---|---|
POST /api/cli-login/start | 10 requests per minute per IP |
POST /api/cli-login/poll | 60 requests per minute per IP |
Summary
| Endpoint | Method | Purpose |
|---|---|---|
/api/cli-login/start | POST | Start login, get device_code and verification URL |
/api/cli-login/poll | POST | Poll for approval status and retrieve API key |
/cli-login/verify?code=... | GET | Browser page where the user approves |