Tech AI LabsAI Growth Infrastructure for Business

How to use the OpenAI API: from key to first request

By the Tech AI Labs teamChecked 22 November 20264 min read
How to use the OpenAI API: from key to first request: a practical workflow overview

The OpenAI API lets your software use OpenAI models from your own software for tasks such as text generation and analysis. This guide takes you from zero to a working first request, then shows how to keep costs and risk under control.

The short answer

Create a provider account and API key, check billing and choose a model available to your project. Keep the key server-side, install the SDK and make a small Responses API request. Validate the result and keep the first workflow draft-only.

Step 1: Create the key safely

In the platform dashboard, create a new secret key and store it somewhere safe immediately - it is shown only once. Treat it like a password: never paste it into client-side code, public repositories or chat tools. Use environment variables or a secrets manager.

Step 2: Make your first request

Install the official Python SDK in your project environment with pip install openai. Set OPENAI_API_KEY securely outside the script and set OPENAI_MODEL to a model ID available to your project. This sample uses the Responses API:

python
import os
from openai import OpenAI

client = OpenAI()
response = client.responses.create(
    model=os.environ["OPENAI_MODEL"],
    input="Summarise this fictional ticket: I need to move my appointment."
)
print(response.output_text)

This documentation-based example has not been executed with paid API credits. Running it can incur charges. Review current pricing and your model access first.

A bounded worked example with explicit source, task and rules.
Guide diagram: A bounded worked example with explicit source, task and rules.

Worked example, fictional data:

Step 3: Ask for structured output

The API becomes genuinely useful when answers come back as data, not prose. Define a JSON schema for what you want (for example: name, email, company, intent) and validate the model output against that schema before using it - this is how lead-qualification and document-extraction workflows are built.

Step 4: Control costs from day one

API usage is pay-as-you-go, priced per million tokens (roughly, per chunk of text processed). Set a project spend limit and check whether Enforce a hard limit is enabled. Alerts alone do not stop requests, and enforcement can lag. Add application-side controls, choose the smallest model that meets the task, and check caching support in the current docs. Check the current per-model prices on the official page before building: https://openai.com/api/pricing/

Step 5: Design for failure

APIs time out, rate limits bite, and models occasionally return junk. Build retries with backoff, validate every response before using it, and keep a human review step for anything customer-facing until the workflow earns trust.

Five common integration mistakes

  1. 1Sending a whole database worth of context instead of the five lines that matter.
  2. 2Using the biggest model for tasks a small one handles.
  3. 3No spending cap - the first surprise bill teaches this.
  4. 4Trusting output blindly instead of validating structure and facts.
  5. 5Putting the key in frontend code, where anyone can steal it.

Try one bounded work task

Build a draft-only summarizer for fictional support tickets. Accept one ticket as input, request a short summary, validate the response and show it to a person. Do not automatically update a live ticket or send a reply in the first version.

Example brief: Summarise this fictional support ticket in one sentence. Preserve the issue and requested next step. If the ticket does not state a fact, do not add it.

A source-to-claim check showing what is and is not supported.
Guide diagram: A source-to-claim check showing what is and is not supported.

Review the example:

Check the output before using it

Keep the key server-side, use a model available to your project, inspect the returned output and usage, and handle errors without unbounded automatic retries. Check whether your project spend limit is monitoring only or enforcing a hard limit. OpenAI says enforcement is not instantaneous, so retain application-side controls.

Choose the right next tool

For adjacent tasks, read ChatGPT if you only need the app, the Anthropic Messages API, Claude for a non-code document workflow. These are different workflows, not a ranking of the tools.

Official reference

This guide uses the current official getting-started guidance. Features and account access can change. Check the provider before buying a plan or connecting business data.

For the current control names and enforcement limits, read OpenAI project spend-limit guidance.

FAQ

Quick answers

How do I get an OpenAI API key?

Create an account at platform.openai.com, add billing, and create a secret key in the API keys section. Store it immediately - it is shown only once.

How is the OpenAI API billed?

Pay-as-you-go, priced per million tokens processed, with rates varying by model. Budgets and usage limits can be set in the dashboard.

Can I use the OpenAI API without coding?

Yes - automation tools like Zapier, n8n or Make can call the API through visual workflows, or an agency can build the integration for you.

Which endpoint does this tutorial use?

It uses the Responses API for a basic text request. Check the current official quickstart and the models available in your project.

Where should an API key live?

Store it in a secrets manager or server-side environment variable. Do not place it in browser code, a public repository or an example that other people can read.

Is a project budget a guaranteed hard spending cap?

A project spend limit can monitor spending or enforce a hard limit. Alerts do not stop requests. Check whether Enforce a hard limit is enabled; OpenAI says enforcement is not instantaneous and recorded spending can slightly exceed the configured limit.

What should I do with a rate-limit error?

Check the error and account limits. Use bounded retries with backoff rather than a tight retry loop, and stop when continuing would repeat the same failure.

Were the sample requests executed for this article?

No. They are documentation-based examples, not a paid API test. Choose an available model and review current billing terms before running them.

IN THE DIRECTORY

See how this tool compares, what it costs and what it is best for.

Open the tool page →
How to use Claude: a practical guide for real work: article illustration
TUTORIALS

How to use Claude: a practical guide for real work

Read →
How to use the Anthropic API: Claude in your own product: article illustration
TUTORIALS

How to use the Anthropic API: Claude in your own product

Read →
How to use ChatGPT: a practical guide for real work: article illustration
TUTORIALS

How to use ChatGPT: a practical guide for real work

Read →

Keep exploring

Move from research to a useful workflow. Check the source, learn the steps, then discuss implementation.

Discuss your workflow