Skip to main content
POST
/
auth
/
keys
Exchange authorization code for API key
curl --request POST \
  --url https://openrouter.ai/api/v1/auth/keys \
  --header 'Authorization: Bearer <token>' \
  --header 'Content-Type: application/json' \
  --data '
{
  "code": "auth_code_abc123def456",
  "code_challenge_method": "S256",
  "code_verifier": "dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk"
}
'
import requests

url = "https://openrouter.ai/api/v1/auth/keys"

payload = {
"code": "auth_code_abc123def456",
"code_challenge_method": "S256",
"code_verifier": "dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk"
}
headers = {
"Authorization": "Bearer <token>",
"Content-Type": "application/json"
}

response = requests.post(url, json=payload, headers=headers)

print(response.text)
const options = {
method: 'POST',
headers: {Authorization: 'Bearer <token>', 'Content-Type': 'application/json'},
body: JSON.stringify({
code: 'auth_code_abc123def456',
code_challenge_method: 'S256',
code_verifier: 'dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk'
})
};

fetch('https://openrouter.ai/api/v1/auth/keys', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));
<?php

$curl = curl_init();

curl_setopt_array($curl, [
CURLOPT_URL => "https://openrouter.ai/api/v1/auth/keys",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'code' => 'auth_code_abc123def456',
'code_challenge_method' => 'S256',
'code_verifier' => 'dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk'
]),
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>",
"Content-Type: application/json"
],
]);

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
package main

import (
"fmt"
"strings"
"net/http"
"io"
)

func main() {

url := "https://openrouter.ai/api/v1/auth/keys"

payload := strings.NewReader("{\n \"code\": \"auth_code_abc123def456\",\n \"code_challenge_method\": \"S256\",\n \"code_verifier\": \"dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk\"\n}")

req, _ := http.NewRequest("POST", url, payload)

req.Header.Add("Authorization", "Bearer <token>")
req.Header.Add("Content-Type", "application/json")

res, _ := http.DefaultClient.Do(req)

defer res.Body.Close()
body, _ := io.ReadAll(res.Body)

fmt.Println(string(body))

}
HttpResponse<String> response = Unirest.post("https://openrouter.ai/api/v1/auth/keys")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"code\": \"auth_code_abc123def456\",\n \"code_challenge_method\": \"S256\",\n \"code_verifier\": \"dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk\"\n}")
.asString();
require 'uri'
require 'net/http'

url = URI("https://openrouter.ai/api/v1/auth/keys")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true

request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"code\": \"auth_code_abc123def456\",\n \"code_challenge_method\": \"S256\",\n \"code_verifier\": \"dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk\"\n}"

response = http.request(request)
puts response.read_body
{
  "key": "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",
  "user_id": "user_2yOPcMpKoQhcd4bVgSMlELRaIah"
}
{
"error": {
"code": 400,
"message": "Invalid request parameters"
}
}
{
"error": {
"code": 403,
"message": "Only management keys can perform this operation"
}
}
{
"error": {
"code": 500,
"message": "Internal Server Error"
}
}

Authorizations

Authorization
string
header
required

API key as bearer token in Authorization header

Body

application/json
code
string
required

The authorization code received from the OAuth redirect

Example:

"auth_code_abc123def456"

code_challenge_method
enum<string> | null

The method used to generate the code challenge

Available options:
S256,
plain,
null
Example:

"S256"

code_verifier
string

The code verifier if code_challenge was used in the authorization request

Example:

"dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk"

Response

Successfully exchanged code for an API key

key
string
required

The API key to use for OpenRouter requests

Example:

"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"

user_id
string | null
required

User ID associated with the API key

Example:

"user_2yOPcMpKoQhcd4bVgSMlELRaIah"