How to Make Your First AI API Request

What you need, what to send and what comes back: a step-by-step guide to your first AI API request, with dated OpenAI, Google and Mistral formats.

You have typed thousands of prompts into a chat window. Now you want your own code, or even a single line in a terminal, to send a prompt to a model and get the answer back. That is an API request, and the first one is mostly about getting a few details exactly right: where to send it, how to prove it is yours, and what to put inside.

Here is the short version. You need an account with an AI provider, a way to pay or a free tier that the provider offers, and an API key saved in an environment variable on your computer rather than pasted into your code. You then copy the address and model name from the provider’s own quickstart, send one POST request whose body is a small piece of JSON naming the model and your prompt, and read the JSON that comes back: the answer text is in one field and the token counts are in another [1, 2, 3]. This guide walks through each part using a hypothetical test prompt: asking a model for three name ideas for a neighborhood book club.

If you are not sure what an API is yet, start with what an AI API is and how apps reach a model. If you would rather not write any code at all, whether you need to code to use an AI API lays out the other routes.

TL;DR

  • You need three things first: a provider account with billing or a free tier, an API key stored in an environment variable, and a terminal or a few lines of code [1, 2, 3].
  • Copy details from the provider, not from memory or old tutorials. Endpoints, headers and model names differ between providers and change over time [4, 5].
  • Keep the first test small: a short prompt and a low cap on output length [6, 7].
  • The answer and the token counts arrive together. Each provider puts the text and a usage summary in documented fields of a JSON response [6, 2, 3].
  • Learn four errors in advance: a wrong key, a wrong model name, no credit and too many requests. Read the message as well as the status code, because codes overlap: OpenAI uses 429 for both of the last two [8, 9].

What You Need Before You Start

A few terms first. An API key is a secret string that identifies your account or project; Google says its key is used to authenticate requests, enforce security limits and track usage to your account [2]. A request is the message your code sends, and the response is the message that comes back. Both travel over HTTP, the protocol the web uses [10].

An Account, and Billing or a Free Tier

Each provider handles payment differently, so read your provider’s billing page before your first call. What follows is what the providers’ own pages said when checked on November 22, 2026 [11, 12, 3].

  • OpenAI: new API accounts use prepaid billing, meaning you buy credits before use and the balance falls as you use the API; any free credits are spent first [11]. The minimum first purchase is listed as $5 [11]. OpenAI’s quickstart calls the first call “a free test API request” and then points to adding credits, but the pages we read did not say how much free use a new account gets [1]. Separately, OpenAI says eligible ChatGPT Pro and Business subscriptions can claim API credits by linking the subscription to an API organization the subscriber owns, so a chat plan is not the same thing as API access [13].
  • Google (Gemini API): new accounts begin on a Free Tier that covers certain models up to free-tier rate limits, and some projects need verification first [12, 2]. Moving to the Paid Tier means linking a billing account and, typically, prepaying at least $5 or the equivalent; Google’s setup steps say some accounts are instead offered a choice of Postpay or placed on Postpay for a transition period [12].
  • Mistral: its quickstart asks for an account on a plan with API access [3].

One data point matters before you type anything. Under Google’s Gemini API terms, effective March 23, 2026, content sent through unpaid services such as the free API quota may be used to improve Google’s products and read by human reviewers, and Google says not to submit sensitive, confidential or personal information there [14]. The same terms make an exception for users in the European Economic Area, Switzerland and the UK, where the paid-service data terms apply even to free use [14]. For paid quota, which means using the API through a Cloud project with an active billing account, Google says it doesn’t use prompts or responses to improve its products, though it logs them for a limited period to detect policy violations [14]. These terms cover Google’s Gemini API only, so check your own provider’s; a made-up test prompt sidesteps the issue.

OpenAI also warns that a prepaid balance is not an instantaneous spending cutoff [11]. How to avoid unexpected AI API charges covers the controls that do exist.

A Key, Kept Out of Your Code

An environment variable is a named value your operating system holds for programs to read, so the key never has to appear in your code. OpenAI’s quickstart tells you to export the key as an environment variable, and says its SDKs read it from there automatically [1]; Google’s and Mistral’s quickstarts do the same under their own variable names [2, 3]. Mistral notes that a variable exported in a terminal lasts only for that session [3].

This guide uses a neutral name, AI_API_KEY, for its curl example. In a macOS or Linux terminal running Bash or Zsh, the line looks like this:

export AI_API_KEY="YOUR_API_KEY"

Don’t type that line at the prompt with your real key in it. Bash keeps a list of the commands you type and, when the shell exits, writes it to a history file (by default ~/.bash_history), and Zsh does the same when a history file is set, leaving the key in plain text there [15, 16]. Google’s key page gives a different route: add the export line to the end of your shell’s startup file (~/.bashrc for Bash, ~/.zshrc for Zsh), save it, then run source ~/.bashrc or source ~/.zshrc [17]. OpenAI’s quickstart likewise suggests keeping the key in a file such as .zshrc [1]. Either way the key is stored in a plain-text file, so keep that file out of any repository or shared folder. On Windows, the steps differ. Google’s key page walks through the Environment Variables dialog and says to open a new terminal afterwards [17]. OpenAI’s quickstart uses setx in PowerShell instead [1]; Microsoft says a variable set with setx reaches only command windows opened afterwards [18], and PowerShell’s PSReadLine module keeps a history file of typed commands that skips only lines containing certain words, so a key typed there may be saved [19]. The dialog route avoids typing the key at a prompt.

Copy the key carefully: Mistral, for example, shows the full key only once [3]. Google’s rules are blunt: never check keys into source control such as Git, and never hardcode them in web or mobile apps, where users can extract them [17]. What an API key is and why you should protect it explains the rest, including what to do if one leaks.

A Terminal With curl, or a Few Lines of Code

curl is a command-line tool for sending data to and from a server; its -H option adds a header, and its -d option sends data with the POST method [20]. Windows users may already have it: the curl project says Microsoft ships curl in Windows 10 and 11 [21]. If you prefer a programming language, providers publish an SDK, ready-made code that wraps the same API; OpenAI’s Python package is called openai [1], and Google offers Python and JavaScript SDKs alongside plain REST [2].

Step 1: Find Your Provider’s Quickstart and API Reference

Providers publish two documents you will use constantly, and OpenAI’s and Google’s versions are cited throughout this guide [1, 6, 2, 9]. The quickstart walks through a first call. The API reference lists every field a request can contain and every field a response can return.

Trust these over blog posts and old videos, because the details move. In late November 2026, Google described its newer Interactions API as the recommended interface for new projects and called the older generateContent format legacy, though still supported [5]. Model names move too: Google’s deprecations page says that from December 1, 2026, requests to gemini-2.5-pro will be routed automatically to gemini-3.8-flash and billed at that model’s rates [4].

So copy three things from the current documentation: the endpoint (the exact web address for the function you want), the name of the header that carries your key, and a model name. On the day this guide was checked, Nov 22, 2026, OpenAI’s quickstart used gpt-6.2-astra, which its models page suggested as a starting point and its deprecations page did not list for retirement [1, 22, 23]. Google’s quickstart used gemini-4-hydrogen, released November 12, 2026, with no shutdown date announced [2, 4]. Those names are examples that will date, not recommendations; how to choose an AI model for a simple application covers choosing one.

Step 2: Build the Smallest Request That Can Work

Strip a request down and it has four parts.

  • Endpoint: the address you copied, held here in a placeholder variable, $AI_API_URL.
  • Method: POST, the HTTP method that asks the server to process the data you send [10].
  • Headers: labeled lines sent with the request. One says the body is JSON; another carries your key.
  • Body: the content itself, written in JSON, a lightweight text format for structured data in which an object sits inside curly brackets, field names are strings, and strings sit inside double quotation marks [24].

Here is an illustrative body for the book club test. Its fields follow OpenAI’s Responses API format as documented on Nov 22, 2026; other providers’ formats differ, as the table below shows [6]:

{"model": "MODEL_NAME", "input": "Suggest three names for a neighborhood book club.", "max_output_tokens": 100}

The last field is a ceiling on how long the reply can be. Usage is counted in tokens, the units providers use to measure the text a model reads and writes; OpenAI’s response reports input and output tokens separately [6], and how AI API pricing works explains them. OpenAI’s max_output_tokens caps the tokens a response may generate, counting both visible output and reasoning tokens, with a documented minimum of 16 [6]. Google accepts a max_output_tokens value inside a generation_config object [7]. A short prompt plus a low cap keeps your first test small, so a mistake costs little. Because OpenAI counts reasoning tokens against the same cap, a very low limit on a model that reasons before answering leaves little room for the visible reply [6]. If the text comes back cut short, or OpenAI’s response shows a status of incomplete with the reason max_output_tokens, raise the cap [6].

Providers do not share one format. This table shows how the three providers’ quickstarts and API references differed on Nov 22, 2026 [1, 6, 2, 3]:

Part of the requestOpenAIGoogle (Gemini API)Mistral
Header carrying the keyAuthorization: Bearer plus the key [6]x-goog-api-key [2]Authorization: Bearer plus the key [3]
Other headers in the exampleContent-Type: application/json [6]Content-Type and Api-Revision: 2026-05-20 [2]Content-Type: application/json [3]
Body fields in the first examplemodel, input [1]model, input [2]model, messages (a list with a user message) [3]
Output capmax_output_tokens [6]max_output_tokens in generation_config [7]max_tokens (in the API reference, not the quickstart) [25]
Key variable in the quickstartOPENAI_API_KEY [1]GEMINI_API_KEY [2]MISTRAL_API_KEY [3]

Check your own provider’s reference before borrowing anything from another column.

Step 3: Send It

With the key in $AI_API_KEY and the endpoint in $AI_API_URL, the whole request fits on one line. This is an illustrative command for a macOS or Linux terminal, in the shape OpenAI documents; it has not been run for this guide, and your reply will differ:

curl -X POST "$AI_API_URL" -H "Content-Type: application/json" -H "Authorization: Bearer $AI_API_KEY" -d '{"model": "MODEL_NAME", "input": "Suggest three names for a neighborhood book club.", "max_output_tokens": 100}'

-X POST sets the method. Each -H adds a header [20]. The $AI_API_KEY inside double quotes is filled in from your environment when the command runs, and Bash records the command in its history before that substitution, so the history shows the variable name, not the key [15]. -d sends the JSON body [20]. Replace MODEL_NAME with the model name you copied. The quoting and $ variable syntax are for macOS and Linux shells; on Windows, start from your provider’s own Windows instructions, such as those cited in the key section [1, 17].

To adapt it for Google’s REST format, send the key as x-goog-api-key: $AI_API_KEY with no “Bearer”, add the Api-Revision header, and nest the cap inside generation_config [2, 7]. For Mistral’s chat completions endpoint, replace input with a messages list and max_output_tokens with max_tokens [3, 25].

If you would rather use an SDK, OpenAI’s Python quickstart reduces the call to a few lines after installing the openai package; the central line looks like this, following OpenAI’s documentation as of Nov 22, 2026 [1]:

response = client.responses.create(model="MODEL_NAME", input="Suggest three names for a neighborhood book club.")

The SDK reads the key from the environment, so it does not appear in the code, but only under its own variable name: OpenAI’s quickstart sets OPENAI_API_KEY, Google’s libraries look for GEMINI_API_KEY or GOOGLE_API_KEY, and Mistral’s SDKs read MISTRAL_API_KEY [1, 17, 3]. If you set only the neutral AI_API_KEY for the curl line, set your provider’s own variable as well before trying the SDK.

In a real app, this call belongs on a server you control, not in a web page or mobile app [17]. The parts behind an AI feature shows where that server sits, and what happens during an AI API request follows one request from start to finish. Securing a whole application goes further than this guide; BuildAIQ’s Building AI Applications guide on privacy and security covers it.

Step 4: Read What Comes Back

A successful call returns JSON with more fields than you need today. Here is a trimmed, illustrative response in OpenAI’s shape; the names and token counts are invented for this example:

{"id": "RESPONSE_ID", "status": "completed", "output": [{"type": "message", "role": "assistant", "content": [{"type": "output_text", "text": "1. Porch Light Readers 2. The Next Chapter Club 3. Between the Covers"}]}], "usage": {"input_tokens": 18, "output_tokens": 22, "total_tokens": 40}}

The text. In OpenAI’s Responses API, generated content sits in an output array whose length and order depend on the model’s response, so OpenAI suggests the SDKs’ output_text property rather than assuming the first item is the answer [6]. Google’s REST response returns the full interaction, including metadata, usage statistics and a step-by-step history, and its SDKs offer output_text too [2]. Mistral puts the reply in choices[0].message.content [3].

The usage. OpenAI’s response includes a usage object with input, output and total tokens [6]. Google’s example shows total_input_tokens and total_output_tokens [2], and Mistral’s response also includes usage [3]. These counts show how much text each test used, so glance at them every time; how AI API pricing works shows how providers turn them into a charge.

Run the same request twice and the wording will likely change; Mistral says reply text varies between runs [3]. A successful response also says nothing about whether the content is right: a 200 means the request worked, not that the answer is accurate. For anything beyond a test, use how to check whether an AI answer is correct.

If the First Request Fails

Every HTTP response carries a three-digit status code. Codes in the 200s mean success, the 400s mean the client seems to have made an error, and the 500s mean a problem on the server’s side [10]. A 429 means too many requests in a given time, which is called rate limiting [26]. The provider also sends an error message in the response body, and Google’s error responses include a machine-readable code and a human-readable message [9]. Read the message before changing anything.

The usual first problems, one line each:

  • 401, key missing or wrong: the request lacked valid credentials [10]. OpenAI lists a typo or extra space in the key among the causes [8], and Mistral suggests checking that the variable holds the full key in the same terminal where you run curl [3].
  • 404, model not found: Google returns model_not_found for an unknown model name [9], and OpenAI’s Python library raises a NotFoundError for a resource that does not exist [8]. Copy the name again from the current models page.
  • No credit or billing: OpenAI returns a 429 with the code credit_balance_exhausted when prepaid credits run out [8]; Google returns a 402 payment_required when the prepay balance is depleted [9]. Both say retrying won’t help until credits are added [8, 9].
  • 429, too many requests: you went over a rate limit, or a daily quota or spend limit, and the error message says which [8, 9]. For a rate limit, wait before trying again; for a spend limit, OpenAI says retrying won’t help [8]. What rate limits are explains the limits.
  • 400, bad request: for example, a malformed body or an unknown parameter [10, 9]. Check for missing quotation marks, a stray comma or a field name copied from another provider.
  • 500s: the server reports a problem on its own side [10], so check the provider’s status before changing your request.

For a full triage order, safe retries and what to do during an outage, see what to do when an AI API stops working.

Where to Go Next

Once one request works:

Practical Takeaways

This checklist is BuildAIQ editorial guidance, built on the provider documentation cited above.

  • Before the call: confirm your account’s billing or free-tier status on the provider’s own page, and read its data terms for that tier [11, 12, 14].
  • Keep the key in an environment variable set from your shell’s startup file or your provider’s recommended method, never in code or a repository [1, 17], and keep it out of chats and screenshots.
  • Copy the endpoint, key header and model name from today’s quickstart, not from an older tutorial [4, 5].
  • Keep the first prompt short and set an output cap [6, 7].
  • Read two fields in the response: the text and the usage counts [6, 2, 3].
  • When it fails, read the status code and the message first, then fix the key, model name, billing or request body as the code suggests [8, 9].
  • Treat a successful response as delivered, not verified.

You now have the full shape of a working request: the prerequisites, the four parts of the call, where the answer lands and what the first errors mean. The next guide, on connecting an AI API to a no-code tool, shows the same request built without a terminal.

Sources

  1. Developer quickstart. OpenAI, accessed 2026-11-22.
  2. Getting started (Gemini API). Google, accessed 2026-11-22.
  3. Quickstart: create your first API key. Mistral AI, accessed 2026-11-22.
  4. Deprecations (Gemini API). Google, accessed 2026-11-22.
  5. Interactions API (overview). Google, accessed 2026-11-22.
  6. Create a model response (Responses API reference). OpenAI, accessed 2026-11-22.
  7. Text generation (Gemini API). Google, accessed 2026-11-22.
  8. Error codes. OpenAI, accessed 2026-11-22.
  9. API errors (Interactions API). Google, accessed 2026-11-22.
  10. RFC 9110: HTTP Semantics. IETF / RFC Editor, 2022-06, accessed 2026-11-22.
  11. Setting up and managing prepaid API billing. OpenAI Help Center, accessed 2026-11-22.
  12. Billing (Gemini API). Google, accessed 2026-11-22.
  13. API credits for ChatGPT subscribers. OpenAI, accessed 2026-11-22.
  14. Gemini API Additional Terms of Service. Google, 2026-03-23, accessed 2026-11-22.
  15. Bash History Facilities (Bash Reference Manual, section 9.1). GNU Project / Free Software Foundation, accessed 2026-11-22.
  16. Parameters Used By The Shell (Zsh manual). Zsh project, accessed 2026-11-22.
  17. API keys (Gemini API). Google, accessed 2026-11-22.
  18. setx. Microsoft Learn, 2026-02-16, accessed 2026-11-22.
  19. about_PSReadLine. Microsoft Learn, 2026-06-16, accessed 2026-11-22.
  20. curl.1 the man page. curl project, accessed 2026-11-22.
  21. curl for Windows. curl project, accessed 2026-11-22.
  22. Models. OpenAI, accessed 2026-11-22.
  23. Deprecations. OpenAI, accessed 2026-11-22.
  24. RFC 8259: The JavaScript Object Notation (JSON) Data Interchange Format. IETF / RFC Editor, 2017-12, accessed 2026-11-22.
  25. Chat Completion API (API reference, POST /v1/chat/completions). Mistral AI, accessed 2026-11-22.
  26. RFC 6585: Additional HTTP Status Codes. IETF / RFC Editor, 2012-04, accessed 2026-11-22.