Using the API

Connect ChatGPT
to your own app.

Six steps from zero to a working call, the code in three languages, and a live box so you can feel the request/response loop before you write any of it.
Live exampleruns on our key

This demo calls a model from our server so you never expose a key. In your own app, the call looks exactly like the code below.

Start here — guided setup

Setup wizardstep 1 of 5

01Create an API account

An API account is separate from a ChatGPT Plus subscription. Sign up (or sign in) at the OpenAI platform, then add a payment method — API access is pay-as-you-go and a test prompt costs a fraction of a cent.

Open platform.openai.com

Run it with your real key

Your own keyreal OpenAI call

Your key is sent to our server for this one request, forwarded straight to OpenAI, and never stored or logged. Token counts and cost above come back from OpenAI itself.

Setup steps

01

Create an API account

Sign up at platform.openai.com — this is separate from your ChatGPT subscription and billed by usage. Add a payment method and set a low monthly limit while you experiment.

02

Create a secret key

In API keys, create a new secret key and copy it once — it is never shown again. Treat it like a password: one key per project, and revoke it the moment it leaks.

03

Keep the key on a server

Never put a key in front-end code or a mobile app; anyone can read it. Store it as an environment variable and call the API from your backend, which then talks to your app.

04

Make your first call

One HTTP POST is all it takes. The messages array carries a system message (who the assistant is) and your user message (the prompt itself).

05

Tune the knobs that matter

model picks capability versus cost, temperature controls how adventurous the wording is (0.2 factual, 0.8 creative), and max_tokens caps the reply length and your bill.

06

Handle failure like it will happen

Expect 429 rate limits and 5xx blips: retry with a short backoff, cap the attempts, and show the real error in your UI instead of a silent empty answer.

The first call

curl https://api.openai.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -d '{
    "model": "gpt-4o-mini",
    "temperature": 0.4,
    "messages": [
      { "role": "system", "content": "You are a concise editor." },
      { "role": "user",   "content": "Rewrite this in two lines: ..." }
    ]
  }'

What will this cost me?

Cost calculatorUSD, list prices

Model

Typical call size

$20

Per prompt

$0.00054

a page of context

Per 1,000 prompts

$0.5400

same size

Prompts for $20

37,037

in a month

That is roughly

1,234/day

every day

Tokens are chunks of text — about 4 characters, so 750 words ≈ 1,000 tokens. You pay for what goes in and what comes out, and output usually costs 4× more than input. Prices shown are OpenAI list prices for planning only; check their pricing page before you commit.

Compare these prices with Gemini, Claude and Llama →

Keep the bill boring

  • — Start on a small, cheap model; upgrade only where quality clearly suffers.
  • — Cap max_tokens on every call; runaway replies are the usual surprise.
  • — Cache repeat answers instead of asking the same question twice.
  • — Set a hard monthly spend limit in your API dashboard on day one.

Keep going