---
title: "How to Use an LLM API in 2026: Five Steps to Your First Reply"
description: "How to use an LLM API in five steps: get a key, pick a base URL, choose a model, send a request, read the response. Minimal curl, Python and Node examples."
url: https://cdnping.org/en/blog/llm-api-first-request-guide
language: en
published: 2026-11-23
category: "AI concepts"
tags: ["LLM API","API key","beginner guide","curl","OpenAI-compatible API"]
source: cdnping.org
license: Free to quote and summarize with a link to the url above.
---
# How to Use an LLM API in 2026: Five Steps to Your First Reply

How do you use an LLM API? At its core, you send an HTTP request with a secret key to a URL. The request names a model and contains your question, and the server returns a JSON response with the reply text inside. The whole process breaks into five steps: get a key, pick a base URL, choose a model name, send the request, and read the response. This guide walks through them in that order.

## Step 1: Where does the key come from, and what should you watch for?

An API key is a string the server uses to identify you and decide whom to bill. You create it in the model provider's developer console. Where to find it and whether you must add a payment method depends on the provider, so check the official docs.

A few rules:

- A key is usually shown only once, at creation. Save it right away.
- Don't commit it to a code repository or paste it into a group chat. The usual habit is to keep it in an environment variable.
- A web chat account and an API account are separate things. Their quotas and billing don't carry over to each other.

More on keys: [What is an API key? Five things beginners should know before using an AI API](/en/blog/what-is-api-key-beginner).

```bash
export API_KEY="your key"
```

## Step 2: How do you choose a base URL?

The base URL decides who receives your request. Two formats are common today:

| Format | Typical endpoint | Authentication |
| --- | --- | --- |
| OpenAI format | `https://api.openai.com/v1/chat/completions` | Header `Authorization: Bearer <Key>` |
| Anthropic format | `https://api.anthropic.com/v1/messages` | Header `x-api-key: <Key>`, plus `anthropic-version` |

Many other providers and gateways offer an OpenAI-compatible interface. You only swap in the address from their docs, which is why many tools just ask for an "address + key". For the underlying idea, see [What does "OpenAI-compatible API" mean?](/en/blog/openai-compatible-api-explained)

## Step 3: What goes in the model name?

The `model` field in the request body must be a model name the service currently offers. A wrong name returns a 404 or a similar "model not found" error. Models get updated and retired, so don't copy names from old tutorials; copy them from the provider's official model list. To see which of the latest models from each provider are currently available, you can check cdnping's [model monitoring](/en/models). The examples below use `MODEL_NAME` as a placeholder.

## Step 4: How do you send the first request?

Using the OpenAI format as the example, start with curl, which needs no extra dependencies:

```bash
curl https://api.openai.com/v1/chat/completions \
  -H "Authorization: Bearer $API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "MODEL_NAME",
    "messages": [{"role": "user", "content": "用一句话介绍你自己"}]
  }'
```

(The prompt in the examples means "Introduce yourself in one sentence." Use any prompt you like.)

Python (`pip install openai`):

```python
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["API_KEY"], base_url="https://api.openai.com/v1")
resp = client.chat.completions.create(
    model="MODEL_NAME",
    messages=[{"role": "user", "content": "用一句话介绍你自己"}],
)
print(resp.choices[0].message.content)
```

Node (version 18 and above ships with fetch):

```javascript
const res = await fetch("https://api.openai.com/v1/chat/completions", {
  method: "POST",
  headers: {
    "Authorization": `Bearer ${process.env.API_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    model: "MODEL_NAME",
    messages: [{ role: "user", content: "用一句话介绍你自己" }],
  }),
});
const data = await res.json();
console.log(data.choices[0].message.content);
```

To switch to another compatible service, change only the `base_url` / request URL, the key, and the model name. If you use the Anthropic format, the headers, path, and body fields differ (for example, `max_tokens` is required), so follow its official docs. For a line-by-line curl walkthrough, see [Your first AI API call: send a request with curl](/en/blog/first-ai-api-call-tutorial). For the full Claude flow, see [How to use the Claude API](/en/blog/claude-api-beginner-getting-started).

## Step 5: What should you look at in the response?

The response is JSON. In the OpenAI format the reply text is at `choices[0].message.content`; in the Anthropic format it is in the `content` array. Besides the text, watch for:

- `usage`: how many input and output tokens this request used. Your bill is calculated from it. For a breakdown, see [How token billing works](/en/blog/llm-token-billing-explained).
- `finish_reason` (or an equivalent field): if it shows the output was cut off for length, the output limit is too small.

## What if you get an error?

Start with the HTTP status code and the error message in the response body. The table below gives the general HTTP meanings; for specific error codes, follow the official docs of the service you use:

| Status | Common cause | Check first |
| --- | --- | --- |
| 400 | Malformed request body or fields | Is the JSON valid? Do field names match that format's docs? |
| 401 | Invalid or missing key | Header syntax, whether the environment variable took effect, stray spaces |
| 403 | No permission | Does the account or key have access to that model? Is your region supported by the service? |
| 404 | Wrong URL or model name | Did you leave out `/v1` in the path? Does the model name exist? |
| 429 | Requests too fast or quota used up | Slow down; check balance and limits |
| 5xx | Server-side problem | Retry later; check [API status](/en/status) to see whether the official endpoint is healthy |

Connection timeouts or unusually slow responses are often about the network path, not your code. Start with [AI API latency and access from mainland China](/en/learn/ai-api-latency-china), then use the online speed test on the cdnping home page to see the first-connection and round-trip times to each API endpoint from your current network.

## No overseas payment method? How can you get started?

Connecting directly to some overseas providers' APIs often runs into three kinds of barriers: supported sign-up regions, payment methods, and network access. Whether you can use them is up to each provider's official policy; please follow their terms of service and local regulations.

If you just want to get the flow working first, you can look at AGI Plan. It is a multi-provider LLM aggregation gateway with monthly subscriptions, paid in RMB, with no overseas credit card needed. It is compatible with both the OpenAI and Anthropic API formats, so tools like Claude Code and Codex only need one changed base URL. Some models are marked "overseas only"; check the [models page](https://agiplan.dev/models) for the current status. AGI Plan is not an officially authorized channel. AGI Plan is a commercial partner of cdnping. Its getting-started guide is at [agiplan.dev/startup](https://agiplan.dev/startup).

To decide which path to take, use the six dimensions in [How to choose an LLM API](/en/blog/llm-api-selection-checklist).

## What to do next

1. Get a first reply with any of the examples above.
2. Print `usage` and run a few requests to get a feel for how many tokens one call uses.
3. Then connect the editor or tool you normally use: anything that accepts an "address + key" can be set up the way Step 2 describes.
