---
title: "OpenAI APIs and the others"
summary: "Providers expose HTTP APIs. You send JSON, they return a completion. You pay per token."
track: "The model"
day: 1
minutes: 14
author: "Akash Panchal"
url: https://ai-sdk-patterns.dev/learn/fundamentals/day-1/provider-apis
dateModified: 2026-08-29
---

# OpenAI APIs and the others

Providers expose HTTP APIs. You send JSON, they return a completion. You pay per token.

*The model · Day 1: How AI apps work · ~14 min. Written by [Akash Panchal](https://github.com/akashp1712).*

Canonical: https://ai-sdk-patterns.dev/learn/fundamentals/day-1/provider-apis

## After this topic

You will be able to describe the network call behind “the app uses AI”: who holds the key, what JSON goes out, what comes back, and why you should not copy that JSON by hand for every vendor.

## What an API is here

> An API is a contract over the network. Your server sends HTTPS. The provider sends JSON back. An API key proves who is paying.

In the last topic the model was a function: tokens in, tokens out. In a product that function lives in someone else’s datacenter. You reach it the same way you reach Stripe or GitHub: an HTTPS request from your server.

OpenAI, Anthropic, Google, and others each host models. Each publishes a REST API and an official SDK. The shapes rhyme: which model, a list of messages, optional knobs like temperature and max output tokens. The URLs, header names, and JSON field names do not rhyme. That mismatch is why people get stuck on “the OpenAI tutorial” when their key is for Anthropic.

The request must leave from a server you control. If the browser held the provider key, anyone could copy it and spend your money. In Next.js that server is a Route Handler — a file like app/api/chat/route.ts that runs on Vercel, not in the user’s tab.

- Provider — the company hosting the model (OpenAI, Anthropic, Google, …)
- API key — a secret that authenticates the request and ties it to a bill
- Model id — which snapshot you want (a named GPT, Claude, Gemini, …)
- Usage — input tokens plus output tokens; that is the line item

## What the JSON looks like

You do not need to memorize this object. The AI SDK will build it. Seeing it once makes “call OpenAI” concrete: a POST, a messages array, a completion, a usage object. Anthropic and Google use different field names for the same idea. That is the entire reason the next topic exists.

Read the request top to bottom. model picks the weights. messages is the entire world the model is allowed to see for this call. The response’s message is the new assistant turn. usage is how you were billed, even if you ignore it in code.

### openai-shaped-request.json

```ts
{
  "model": "gpt-4.1-mini",
  "messages": [
    { "role": "user", "content": "Name one TypeScript feature." }
  ]
}

// Typical JSON back (simplified):
{
  "choices": [
    { "message": { "role": "assistant", "content": "Static typing." } }
  ],
  "usage": { "input_tokens": 18, "output_tokens": 4 }
}
```

- **messages is the chat.** A list of roles and content. Tomorrow we treat this as the real data model of a chat app. If a turn is not in this list, the model did not see it.
- **usage is the bill.** input_tokens + output_tokens, each with a price. A long prompt costs even if the reply is “OK”. Output is usually priced higher than input.
- **This JSON is vendor-shaped.** choices[0].message is OpenAI’s envelope. Anthropic uses content blocks. Do not parse this by hand in product code — that is what the SDK is for.

## One key, many providers

You can take a key from OpenAI and call api.openai.com. You can take three keys and maintain three SDKs. On Vercel the default path is AI Gateway: one credential (OIDC on deploy, AI_GATEWAY_API_KEY locally), and a model id like openai/gpt-4.1-mini or anthropic/claude-sonnet-4-5. Gateway routes, bills, and can fail over.

That does not change the physics of the last topic. Gateway still sends tokens to a hosted model and still charges for tokens. It changes how many vendor SDKs you import, and how painful it is to switch models.

We will call models through getModel() in every lesson. It turns DEFAULT_MODEL into that Gateway string. You can still point at a single vendor later. The lesson code will not.

## Why we will not start with raw fetch

You could POST this JSON with fetch. For a one-shot “summarize this string” that even works. Then you want streaming, and you are parsing Server-Sent Events. Then tool calls, and you are looping on finish_reason. Then structured JSON, and you are retrying when the model wraps the object in markdown fences. Then a second provider, and you rewrite the parser.

That work is not where your product is. The AI SDK is the library that already did it, with one TypeScript surface. Next topic: what that surface is, and the one helper every lesson copies.

## Common questions

### What is “OpenAI APIs and the others”?

Providers expose HTTP APIs. You send JSON, they return a completion. You pay per token.

### What will I be able to do after this lesson?

You will be able to describe the network call behind “the app uses AI”: who holds the key, what JSON goes out, what comes back, and why you should not copy that JSON by hand for every vendor.

### How long does this lesson take?

About 14 minutes of reading. It is a free chapter in the AI SDK Patterns TypeScript course.
