How to Build a Chatbot with the OpenAI API

Step-by-step: connect to GPT-4, handle context, deploy in 30 minutes

Ad placeholder (leaderboard)

A chatbot is the fastest way to learn the OpenAI API: one endpoint, a list of messages, and a reply. This tutorial walks through authentication, conversation memory, streaming and deployment, and includes an interactive snippet generator so you can copy starter code in your language.

Step 1 — Authenticate

Everything goes through the Chat Completions endpoint at https://api.openai.com/v1/chat/completions. You authenticate with an API key sent as a Bearer token:

Authorization: Bearer YOUR_API_KEY

The single most important rule: keep the key on a server. A key in browser JavaScript can be read by anyone and used to drain your account. In practice you build a tiny backend route (a serverless function is plenty) that holds the key and forwards requests.

Step 2 — Send messages with history

The API is stateless — it remembers nothing between calls. You provide the whole conversation each time as a messages array of role-tagged objects:

[
  { "role": "system", "content": "You are a concise, friendly assistant." },
  { "role": "user", "content": "What is the OpenAI API?" },
  { "role": "assistant", "content": "It lets you call models like GPT-4 over HTTP." },
  { "role": "user", "content": "How do I add memory?" }
]

The system message defines the persona and rules. Each new user turn is appended, and you append the model’s reply before the next call. That growing array is the chatbot’s memory. As it nears the model’s context limit, trim or summarize the oldest turns.

Step 3 — Stream responses and deploy

A reply that arrives all at once feels slow. Set "stream": true and read the response as server-sent events; each chunk holds a fragment of text you append to the UI for the familiar typing effect.

To deploy, put your forwarding route on a host like Vercel or any Node server, set the API key as an environment variable, and point a minimal frontend at it. You now have a working, hosted chatbot.

Use the generator below to produce a ready-to-paste starter for Node, Python, or plain fetch, then read what a token is and estimate spend with the LLM API Cost Calculator before you launch.

Ad placeholder (rectangle)