Buy credits with a machine payment
curl --request POST \
--url https://openrouter.ai/api/v1/credits/purchases \
--header 'Content-Type: application/json' \
--header 'idempotency-key: <idempotency-key>' \
--data '
{
"credit_amount_usd": "25.00"
}
'import requests
url = "https://openrouter.ai/api/v1/credits/purchases"
payload = { "credit_amount_usd": "25.00" }
headers = {
"idempotency-key": "<idempotency-key>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {'idempotency-key': '<idempotency-key>', 'Content-Type': 'application/json'},
body: JSON.stringify({credit_amount_usd: '25.00'})
};
fetch('https://openrouter.ai/api/v1/credits/purchases', 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/credits/purchases",
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([
'credit_amount_usd' => '25.00'
]),
CURLOPT_HTTPHEADER => [
"Content-Type: application/json",
"idempotency-key: <idempotency-key>"
],
]);
$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/credits/purchases"
payload := strings.NewReader("{\n \"credit_amount_usd\": \"25.00\"\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("idempotency-key", "<idempotency-key>")
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/credits/purchases")
.header("idempotency-key", "<idempotency-key>")
.header("Content-Type", "application/json")
.body("{\n \"credit_amount_usd\": \"25.00\"\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://openrouter.ai/api/v1/credits/purchases")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["idempotency-key"] = '<idempotency-key>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"credit_amount_usd\": \"25.00\"\n}"
response = http.request(request)
puts response.read_body{
"data": {
"created_at": "<string>",
"credit_amount_usd": "<string>",
"failure_reason": "<string>",
"id": "<string>",
"network": "<string>",
"platform_fee_usd": "<string>",
"protocol": "<string>",
"status": "awaiting_payment",
"total_charged_usd": "<string>"
}
}{
"error": {
"code": 400,
"message": "Invalid request parameters"
}
}{
"error": {
"code": 401,
"message": "Missing Authentication header"
}
}{
"setup": "https://openrouter.ai/api/v1/credits/purchases/setup",
"status": 402,
"title": "<string>",
"type": "https://paymentauth.org/problems/payment-required",
"challengeId": "<string>",
"detail": "<string>"
}{
"error": {
"code": 403,
"message": "Only management keys can perform this operation"
}
}{
"error": {
"code": 404,
"message": "Resource not found"
}
}{
"error": {
"code": 409,
"message": "Resource conflict. Please try again later."
}
}{
"error": {
"code": 429,
"message": "Rate limit exceeded"
}
}{
"error": {
"code": 500,
"message": "Internal Server Error"
}
}{
"error": {
"code": 503,
"message": "The payment transfer could not be confirmed; poll the purchase before retrying",
"metadata": {
"purchase_id": "pi_3PqXyZ2eZvKYlo2C0abc1234"
}
}
}Buy credits with a machine payment
Starts an agent credit purchase paid with a Machine Payments Protocol (MPP) credential: USDC on Tempo, or a card when one is offered. The first call answers 402 with one WWW-Authenticate: Payment ... challenge per payment option; pay one of them and retry the same request, same Idempotency-Key, with the signed credential in Payment-Authorization: Payment ... (or in Authorization: Payment ... when the management key is sent as X-Api-Key). A paid request answers 202: the transfer was broadcast or the card was charged, and the payment is still being confirmed, so credits arrive asynchronously. Poll the URL in the Location header, GET /api/v1/credits/purchases/{id}, until status is credited or failed.
POST
/
credits
/
purchases
Buy credits with a machine payment
curl --request POST \
--url https://openrouter.ai/api/v1/credits/purchases \
--header 'Content-Type: application/json' \
--header 'idempotency-key: <idempotency-key>' \
--data '
{
"credit_amount_usd": "25.00"
}
'import requests
url = "https://openrouter.ai/api/v1/credits/purchases"
payload = { "credit_amount_usd": "25.00" }
headers = {
"idempotency-key": "<idempotency-key>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {'idempotency-key': '<idempotency-key>', 'Content-Type': 'application/json'},
body: JSON.stringify({credit_amount_usd: '25.00'})
};
fetch('https://openrouter.ai/api/v1/credits/purchases', 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/credits/purchases",
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([
'credit_amount_usd' => '25.00'
]),
CURLOPT_HTTPHEADER => [
"Content-Type: application/json",
"idempotency-key: <idempotency-key>"
],
]);
$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/credits/purchases"
payload := strings.NewReader("{\n \"credit_amount_usd\": \"25.00\"\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("idempotency-key", "<idempotency-key>")
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/credits/purchases")
.header("idempotency-key", "<idempotency-key>")
.header("Content-Type", "application/json")
.body("{\n \"credit_amount_usd\": \"25.00\"\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://openrouter.ai/api/v1/credits/purchases")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["idempotency-key"] = '<idempotency-key>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"credit_amount_usd\": \"25.00\"\n}"
response = http.request(request)
puts response.read_body{
"data": {
"created_at": "<string>",
"credit_amount_usd": "<string>",
"failure_reason": "<string>",
"id": "<string>",
"network": "<string>",
"platform_fee_usd": "<string>",
"protocol": "<string>",
"status": "awaiting_payment",
"total_charged_usd": "<string>"
}
}{
"error": {
"code": 400,
"message": "Invalid request parameters"
}
}{
"error": {
"code": 401,
"message": "Missing Authentication header"
}
}{
"setup": "https://openrouter.ai/api/v1/credits/purchases/setup",
"status": 402,
"title": "<string>",
"type": "https://paymentauth.org/problems/payment-required",
"challengeId": "<string>",
"detail": "<string>"
}{
"error": {
"code": 403,
"message": "Only management keys can perform this operation"
}
}{
"error": {
"code": 404,
"message": "Resource not found"
}
}{
"error": {
"code": 409,
"message": "Resource conflict. Please try again later."
}
}{
"error": {
"code": 429,
"message": "Rate limit exceeded"
}
}{
"error": {
"code": 500,
"message": "Internal Server Error"
}
}{
"error": {
"code": 503,
"message": "The payment transfer could not be confirmed; poll the purchase before retrying",
"metadata": {
"purchase_id": "pi_3PqXyZ2eZvKYlo2C0abc1234"
}
}
}Headers
Body
application/json
Was this page helpful?