OpenAI Platform API Pricing
We provide a API with all OpenAI Platform pricing to be fetched in JSON. Prices are being closely monitored, and updated within reasonable interval after the actual change.
Swagger UI: https://api.openaipricing.com/
Source: https://platform.openai.com/docs/pricing
Usage examples#
The Pricing API provides pricing information for various OpenAI-related services. This document focuses on an example of how to fetch pricing details for text tokens via the /openai/text_tokens endpoint.
Bash (using curl)#
curl -X GET "https://api.openaipricing.com/openai/text_tokens" \
-H "Accept: application/json" | jq
Python (using requests)#
import requests
url = "https://api.openaipricing.com/openai/text_tokens"
response = requests.get(url, headers={"Accept": "application/json"})
if response.status_code == 200:
data = response.json()
print(data)
else:
print("Request failed with status code", response.status_code)
Go#
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
func main() {
url := "https://api.openaipricing.com/openai/text_tokens"
req, err := http.NewRequest("GET", url, nil)
if err != nil {
panic(err)
}
req.Header.Set("Accept", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
if resp.StatusCode == 200 {
body, _ := ioutil.ReadAll(resp.Body)
var data interface{}
if err := json.Unmarshal(body, &data); err != nil {
panic(err)
}
fmt.Printf("%+v\n", data)
} else {
fmt.Println("Request failed with status:", resp.Status)
}
}
JavaScript (Browser Fetch API)#
fetch("https://api.openaipricing.com/openai/text_tokens", {
method: "GET",
headers: {
"Accept": "application/json"
}
})
.then(response => {
if (!response.ok) throw new Error("Network response was not ok " + response.statusText);
return response.json();
})
.then(data => console.log(data))
.catch(error => console.error("Fetch error:", error));
Node.js (using https module)#
const https = require("https");
const options = {
hostname: "api.openaipricing.com",
path: "/openai/text_tokens",
method: "GET",
headers: {
"Accept": "application/json"
}
};
const req = https.request(options, (res) => {
let data = "";
res.on("data", (chunk) => {
data += chunk;
});
res.on("end", () => {
if (res.statusCode === 200) {
try {
const parsedData = JSON.parse(data);
console.log(parsedData);
} catch (e) {
console.error("Parsing error:", e);
}
} else {
console.error("Request failed with status code", res.statusCode);
}
});
});
req.on("error", (e) => {
console.error("Request error:", e.message);
});
req.end();
Perl (using LWP::UserAgent)#
use strict;
use warnings;
use LWP::UserAgent;
use JSON;
my $url = "https://api.openaipricing.com/openai/text_tokens";
my $ua = LWP::UserAgent->new;
my $req = HTTP::Request->new(GET => $url);
$req->header('Accept' => 'application/json');
my $resp = $ua->request($req);
if ($resp->is_success) {
my $data = decode_json($resp->decoded_content);
use Data::Dumper;
print Dumper($data);
} else {
die "HTTP GET error code: ", $resp->code, "\n",
"HTTP GET error message: ", $resp->message, "\n";
}
PowerShell#
$uri = "https://api.openaipricing.com/openai/text_tokens"
$response = Invoke-RestMethod -Uri $uri -Method GET -Headers @{ "Accept" = "application/json" }
$response | ConvertTo-Json -Depth 10
For more endpoints and details, please refer to the full API specification.