Skip to main content
POST
/
auth
/
keys
/
code
Create authorization code
curl --request POST \
  --url https://openrouter.ai/api/v1/auth/keys/code \
  --header 'Authorization: Bearer <token>' \
  --header 'Content-Type: application/json' \
  --data '
{
  "callback_url": "https://myapp.com/auth/callback",
  "code_challenge": "E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM",
  "code_challenge_method": "S256",
  "limit": 100
}
'
import requests

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

payload = {
    "callback_url": "https://myapp.com/auth/callback",
    "code_challenge": "E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM",
    "code_challenge_method": "S256",
    "limit": 100
}
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({
    callback_url: 'https://myapp.com/auth/callback',
    code_challenge: 'E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM',
    code_challenge_method: 'S256',
    limit: 100
  })
};

fetch('https://openrouter.ai/api/v1/auth/keys/code', 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/code",
  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([
    'callback_url' => 'https://myapp.com/auth/callback',
    'code_challenge' => 'E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM',
    'code_challenge_method' => 'S256',
    'limit' => 100
  ]),
  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/code"

	payload := strings.NewReader("{\n  \"callback_url\": \"https://myapp.com/auth/callback\",\n  \"code_challenge\": \"E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM\",\n  \"code_challenge_method\": \"S256\",\n  \"limit\": 100\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/code")
  .header("Authorization", "Bearer <token>")
  .header("Content-Type", "application/json")
  .body("{\n  \"callback_url\": \"https://myapp.com/auth/callback\",\n  \"code_challenge\": \"E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM\",\n  \"code_challenge_method\": \"S256\",\n  \"limit\": 100\n}")
  .asString();
require 'uri'
require 'net/http'

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

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  \"callback_url\": \"https://myapp.com/auth/callback\",\n  \"code_challenge\": \"E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM\",\n  \"code_challenge_method\": \"S256\",\n  \"limit\": 100\n}"

response = http.request(request)
puts response.read_body
{
  "data": {
    "app_id": 12345,
    "created_at": "2025-08-24T10:30:00Z",
    "id": "auth_code_xyz789"
  }
}
{
  "error": {
    "code": 400,
    "message": "Invalid request parameters"
  }
}
{
  "error": {
    "code": 401,
    "message": "Missing Authentication header"
  }
}
{
  "error": {
    "code": 403,
    "message": "Only management keys can perform this operation"
  }
}
{
  "error": {
    "code": 409,
    "message": "Resource conflict. Please try again later."
  }
}
{
  "error": {
    "code": 500,
    "message": "Internal Server Error"
  }
}

Authorizations

Authorization
string
header
required

API key as bearer token in Authorization header

Body

application/json
callback_url
string<uri>
required

The callback URL to redirect to after authorization. Supports https URLs and localhost/127.0.0.1 URLs on any port for local CLI tools.

Example:

"https://myapp.com/auth/callback"

code_challenge
string

PKCE code challenge for enhanced security

Example:

"E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM"

code_challenge_method
enum<string>

The method used to generate the code challenge

Available options:
S256,
plain
Example:

"S256"

expires_at
string<date-time> | null

Optional expiration time for the API key to be created

Example:

"2027-12-31T23:59:59Z"

key_label
string

Optional custom label for the API key. Defaults to the app name if not provided.

Maximum string length: 100
Example:

"My Custom Key"

limit
number<double>

Credit limit for the API key to be created

Example:

100

spawn_agent
string

Agent identifier for spawn telemetry

Example:

"my-agent"

spawn_cloud
string

Cloud identifier for spawn telemetry

Example:

"aws-us-east-1"

usage_limit_type
enum<string>

Optional credit limit reset interval. When set, the credit limit resets on this interval.

Available options:
daily,
weekly,
monthly
Example:

"monthly"

workspace_id
string<uuid>

Optional workspace ID to associate the API key with

Response

Successfully created authorization code

data
object
required

Auth code data

Example:
{
  "app_id": 12345,
  "created_at": "2025-08-24T10:30:00Z",
  "id": "auth_code_xyz789"
}