---
title: "curl OpenAI API: Your First Request Explained Line by Line (2026)"
description: "A copy-paste curl command for the OpenAI API, explained line by line, with Windows cmd and PowerShell quoting and what 401, 404, and 429 mean."
url: https://cdnping.org/en/blog/curl-first-chat-completions-request
language: en
published: 2026-11-23
category: "Developer tools"
tags: ["curl","OpenAI API","Chat Completions","beginner","Windows"]
source: cdnping.org
license: Free to quote and summarize with a link to the url above.
---
# curl OpenAI API: Your First Request Explained Line by Line (2026)

To call the OpenAI API with curl, you only need to send a POST request to `/v1/chat/completions` with your API key and a JSON body. If it returns JSON, your key, network, and endpoint all work, and debugging gets much easier when you later switch to an SDK or a tool.

Below you get the command first, then a line-by-line explanation, and finally Windows quoting problems and common status codes. If you don't have a key yet, start with [Your first AI API call: sending a request with curl](/en/blog/first-ai-api-call-tutorial).

## What does a complete curl command look like?

First put the key in an environment variable instead of writing it into the command:

```bash
export OPENAI_API_KEY="sk-your-key"
```

Then send the request:

```bash
curl https://api.openai.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -d '{
    "model": "your-model-name",
    "messages": [
      {"role": "system", "content": "You are a concise assistant."},
      {"role": "user", "content": "Explain what DNS is in one sentence."}
    ]
  }'
```

Replace "your-model-name" with a model that is actually available to your account. Check the official docs and your console for the current list; this article does not name specific versions.

## What does each line mean?

| Part | Purpose |
|---|---|
| `https://api.openai.com/v1/chat/completions` | The endpoint. The base URL is `https://api.openai.com/v1`; `/chat/completions` is the specific path |
| `-H "Content-Type: application/json"` | Tells the server the body is JSON. Leaving it out often causes a 400 |
| `-H "Authorization: Bearer ..."` | Authentication. There is one space between `Bearer` and the key |
| `-d '...'` | The request body. With `-d`, curl switches to POST automatically |
| `model` | The model to call. A typo returns a 404 or a similar "model does not exist" error |
| `messages` | The conversation array. `system` sets behavior, `user` is your question, `assistant` holds earlier replies |

The trailing `\` on each line is bash's line continuation character. Nothing, not even a space, may follow it.

## Why do I keep getting JSON parse errors on Windows?

Almost always it's quoting. In bash you wrap JSON in single quotes, but the two Windows shells follow different rules.

**cmd**: It doesn't support single quotes. Wrap the whole JSON in double quotes, escape inner double quotes as `\"`, and use `^` to continue lines:

```bat
curl https://api.openai.com/v1/chat/completions ^
  -H "Content-Type: application/json" ^
  -H "Authorization: Bearer %OPENAI_API_KEY%" ^
  -d "{\"model\":\"your-model-name\",\"messages\":[{\"role\":\"user\",\"content\":\"Hello\"}]}"
```

**PowerShell**: `curl` is an alias for `Invoke-WebRequest` by default, which takes completely different arguments, so write `curl.exe`. Use a backtick to continue lines; the JSON can go in single quotes:

```powershell
$env:OPENAI_API_KEY = "sk-your-key"
curl.exe https://api.openai.com/v1/chat/completions `
  -H "Content-Type: application/json" `
  -H "Authorization: Bearer $env:OPENAI_API_KEY" `
  -d '{\"model\":\"your-model-name\",\"messages\":[{\"role\":\"user\",\"content\":\"Hello\"}]}'
```

When PowerShell passes arguments to curl.exe, it still strips some double quotes, so `\"` is kept here as well. If this is too fiddly, save the JSON as `body.json` and use `-d "@body.json"`. It works in all three shells and is the most reliable approach.

## What do the status codes tell you?

| Status | Common cause | What to check first |
|---|---|---|
| 200 | Success | Read `choices[0].message.content` |
| 400 | Malformed JSON or missing fields | Quotes, commas, `model`, `messages` |
| 401 | Invalid, expired, or missing key | Header spelling, the space after Bearer, whether the key was revoked |
| 404 | Wrong path or model name | Whether the URL has an extra or missing `/v1`, whether the model is available |
| 429 | Requests too fast, or quota or usage limits | Balance and usage tier; retry later |
| 5xx | Server-side problem | Check official status first, then decide whether to retry |

For the exact meaning of each error code, rely on the official docs. For how 429 relates to account limits, see [What are OpenAI API usage tiers?](/en/blog/openai-api-usage-tiers-explained) For similar cases where a wrong Base URL causes 401 or 404, see [Cline OpenAI Compatible config returns 401 or 404](/en/blog/cline-openai-compatible-401-404-errors).

If you suspect an official outage, check the [cdnping OpenAI API status page](/en/status/openai), which probes the official entry point from the Cloudflare edge every 5 minutes. If connections are slow or time out, read [How to read AI API latency from China](/en/learn/ai-api-latency-china).

## What should you watch out for with curl?

- **Don't paste your key into screenshots, chats, or public repos.** Command history also keeps it in plain text, which is why you use an environment variable. If it leaks anyway, follow [Your OpenAI API key leaked](/en/blog/openai-api-key-leaked-response) right away.
- **Add `-i` to see response headers and `-v` to see the connection process.** For timeouts or certificate problems, `-v` shows whether it stalls at DNS, TCP, or TLS.
- **A ChatGPT subscription and the API are two separate accounts.** Having Plus doesn't mean the API has any quota. See [ChatGPT Plus vs. the OpenAI API](/en/blog/chatgpt-plus-vs-openai-api-difference).
- **Don't rely on curl for retries in production code.** Timeout and retry settings are more reliable in an SDK; see [How to configure OpenAI SDK timeouts and retries](/en/blog/openai-anthropic-sdk-timeout-retry-config).

## How can you practice without an overseas credit card?

The official OpenAI API requires an official account and a payment method, and it is limited to supported regions, so check the official policy. If you only want to get command-line calls working first, there is one alternative: AGI Plan is a multi-provider LLM aggregation gateway sold as a monthly subscription, paid in RMB with no overseas credit card needed, and it is compatible with both the OpenAI and Anthropic API formats. In the curl command above, swap in the address and key it provides; see its docs for the exact address and model names. It is not an officially authorized channel, and some models are marked as available only outside mainland China; check [agiplan.dev/models](https://agiplan.dev/models) for the current list. AGI Plan is a commercial partner of cdnping, and the sign-up page is at [agiplan.dev/startup](https://agiplan.dev/startup).

## What should you do next?

1. Get one successful run with the `-d "@body.json"` style and confirm you receive a 200.
2. Add `"stream": true` and look at the streaming response format (see the official docs).
3. Run a test from the [cdnping home page](/en) to see first-connection and round-trip times to each provider's API, so you have a latency baseline.
4. Move to an SDK for your first script and let it handle timeouts and retries.
