For AI agents: a documentation index is available at the root level at /llms.txt and /llms-full.txt. Append /llms.txt to any URL for a page-level index, or .md for the markdown version of any page.
ModelsChatRankingsDocs
DocsAPI ReferenceClient SDKsAgent SDKCookbookChangelog
DocsAPI ReferenceClient SDKsAgent SDKCookbookChangelog
  • API Guides
    • Overview
    • Streaming
    • Embeddings
    • Limits
    • Authentication
    • Parameters
    • Errors and Debugging
  • API Reference
      • POSTExchange authorization code for API key
      • POSTCreate authorization code
LogoLogo
ModelsChatRankingsDocs
API ReferenceOAuth

Exchange authorization code for API key

POST
https://openrouter.ai/api/v1/auth/keys
POST
/api/v1/auth/keys
$curl -X POST https://openrouter.ai/api/v1/auth/keys \
> -H "Authorization: Bearer <token>" \
> -H "Content-Type: application/json" \
> -d '{
> "code": "auth_code_abc123def456",
> "code_challenge_method": "S256",
> "code_verifier": "dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk"
>}'
1{
2 "key": "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",
3 "user_id": "user_2yOPcMpKoQhcd4bVgSMlELRaIah"
4}

Exchange an authorization code from the PKCE flow for a user-controlled API key

Was this page helpful?
Previous

Create authorization code

Next
Built with

Authentication

AuthorizationBearer
API key as bearer token in Authorization header

Request

This endpoint expects an object.
codestringRequired
The authorization code received from the OAuth redirect
code_challenge_methodenum or nullOptional
The method used to generate the code challenge
Allowed values:
code_verifierstringOptional

The code verifier if code_challenge was used in the authorization request

Response

Successfully exchanged code for an API key
keystring
The API key to use for OpenRouter requests
user_idstring or null
User ID associated with the API key

Errors

400
Bad Request Error
403
Forbidden Error
500
Internal Server Error