How to Open an AI API: A Practical First Steps Guide
"Opening" an AI API isn't a single click — it's a short sequence: get credentials, pick an endpoint, send one correctly formatted request, and read the response. Most people searching this are stuck somewhere in that sequence, usually because the docs assume you already know HTTP basics or because the provider's onboarding is buried behind billing setup. This guide walks through the actual steps, in order, with working code.
If you just want the shortest path: sign up with a provider, generate a key, and send a POST request with an Authorization header and a JSON body containing your prompt. Everything below explains each part so you don't get stuck on the details that usually trip people up — auth headers, request shape, and streaming.
Step 1: Get an API key
Every AI API — OpenAI, Anthropic's Claude, Google's Gemini, or a wrapper like SubToAPI — requires a key to authenticate requests. This is different from a login password used in a chat UI; it's a long string generated in a dashboard, meant to be passed programmatically.
To get one:
- Create an account with the provider.
- Navigate to the API or developer section of the dashboard (not the consumer app).
- Generate a new key. It's usually shown once — copy it immediately.
- Store it as an environment variable, never hardcoded in source.
export API_KEY="your-key-here"
If you're building on top of Claude specifically and want application-level keys (sub_live_...) with usage tracking per key, per-seat billing, and a dashboard built for teams rather than a single personal account, SubToAPI issues keys designed for that from a Solo, Team, or Scale plan — see pricing for the breakdown.
Step 2: Know your base URL and endpoint
An AI API is just an HTTP service. You send requests to a base URL plus a path, for example:
https://api.subtoapi.app/v1/messages
The path tells the service what you want to do — generate a chat completion, create an embedding, run a tool call, etc. Get this wrong (typo, wrong version number, missing /v1) and you'll get a 404 before you ever reach an authentication error, which confuses a lot of first-time users into thinking their key is broken.
Step 3: Send your first request
The core anatomy of "opening" the API is one HTTP call: method, headers, body.
curl https://api.subtoapi.app/v1/messages \
-H "Authorization: Bearer $SUBTOAPI_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-sonnet-4",
"max_tokens": 300,
"messages": [
{"role": "user", "content": "Explain what an API endpoint is in one paragraph."}
]
}'
Three things matter here:
- Authorization header — almost every AI API uses
Bearer <key>. Missing or malformed headers are the most common reason for a 401. - Content-Type — must be
application/jsonfor JSON bodies, or the server will reject or misparse the payload. - Request body shape — this varies by provider. Some expect a flat
promptstring, others expect amessagesarray with roles. Read the specific docs for the endpoint you're calling — for SubToAPI, the messages docs cover the full schema, and the quickstart walks through a first call end to end.
Step 4: Read and handle the response
A successful call returns a JSON object with the generated content plus metadata — token counts, stop reason, model used. In JavaScript:
const response = await fetch("https://api.subtoapi.app/v1/messages", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.SUBTOAPI_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
model: "claude-sonnet-4",
max_tokens: 300,
messages: [{ role: "user", content: "List three uses of an AI API." }]
})
});
const data = await response.json();
console.log(data.content[0].text);
console.log(data.usage); // token counts for billing/monitoring
Check response.ok before parsing — a non-200 status usually means an auth problem, a malformed body, or a rate limit, and each returns a different error shape you should log.
Step 5: Move to streaming once the basic call works
A single request/response round trip is fine for short outputs, but for anything longer, streaming tokens back as they're generated makes your app feel responsive instead of frozen for several seconds. This is usually the next thing people need once the basic API is "open" and working:
const response = await fetch("https://api.subtoapi.app/v1/messages", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.SUBTOAPI_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
model: "claude-sonnet-4",
max_tokens: 500,
stream: true,
messages: [{ role: "user", content: "Write a short changelog entry." }]
})
});
const reader = response.body.getReader();
// read chunks as server-sent events, append text incrementally
Full details on event types and reconnect behavior are in the streaming docs.
Step 6: Add tool calls if your app needs to act, not just answer
Once your API connection works and you're getting text back reliably, the next common step is giving the model access to tools — functions it can call to fetch data or trigger actions in your app, with the results fed back into the conversation. This is documented at /docs/tools and follows the same request/response pattern, just with an added tools array in the request and a tool_use block in the response you need to handle.
Common mistakes when opening an AI API for the first time
- Testing in a browser console — CORS and exposed keys make this unreliable and insecure. Use a server or a terminal.
- Skipping
max_tokens— some APIs require it explicitly; omitting it causes a validation error, not a sensible default. - Ignoring rate limits — a burst of test requests can get you throttled. Space out calls while debugging.
- Hardcoding keys in client-side code — anyone can read your JavaScript bundle. Keys belong in server environments only.
Questions
Do I need a credit card to open an AI API account? Most providers require billing details before issuing a production key, though many offer a free trial period first — check the specific provider's signup flow.
What's the difference between opening an API and opening a chat app? A chat app is a finished UI; an API is raw programmatic access you integrate into your own product, authenticated with a key instead of a login session.
Can I test an AI API without writing code? Yes — tools like curl from a terminal, or Postman, let you send requests and inspect responses before building any application logic around them.