AI 6 min

Using the OpenAI API in Your Application

Short answer

The OpenAI API gives developers programmatic access to models like GPT-4o via standard HTTP requests. You authenticate with an API key, call endpoints such as chat completions or embeddings, and pay per token. Rate limits apply per tier, so production integrations need error handling and retry logic.

The OpenAI API lets you add AI capabilities to any application via HTTP requests. Here is how authentication, endpoints, tokens, and costs work.

What the OpenAI API Is

An API (application programming interface) is a standardized way for two software systems to communicate. The OpenAI API is a REST API: you send an HTTP request with JSON data and receive a JSON response. There is no installation required. Any language that can make HTTP calls, Python, JavaScript, Go, PHP, works out of the box.

OpenAI also publishes official client libraries for Python and Node.js that wrap the raw HTTP calls in cleaner syntax. These libraries handle retries and streaming out of the box, which makes them the practical choice for most projects. If you use a workflow tool like n8n, you can also connect directly to an AI model without writing code.

Authentication with API Keys

Every request must include an API key in the Authorization header. You generate keys in the OpenAI dashboard under your account. Treat your key like a password: never commit it to a public repository and never expose it in frontend JavaScript that runs in the browser.

For server-side applications, store the key in an environment variable and read it at runtime. For serverless functions or edge workers, use the secret management tool provided by your hosting platform. If a key is compromised, revoke it immediately in the dashboard and generate a new one.

Key Endpoints

The two endpoints you will use most often are:

Chat completions (POST /v1/chat/completions): The standard endpoint for text generation. You send an array of messages with roles (system, user, assistant) and the model returns a completion. This powers chatbots, summarization, classification, drafting, and most other language tasks.

Embeddings (POST /v1/embeddings): Converts text into a vector, a list of numbers representing the meaning of the text. Embeddings are the foundation of retrieval-augmented generation (RAG) systems where you search a knowledge base before generating an answer.

Other endpoints cover image generation, text-to-speech, speech-to-text, and fine-tuning, but chat completions and embeddings account for the majority of API usage.

Tokens and Costs

The API bills by token, not by request or character. A token is roughly four characters of English text. The sentence “The quick brown fox” is about five tokens. Longer prompts cost more, and longer outputs cost more still.

Each model has a context window measured in tokens: the maximum combined length of input plus output for one request. If your prompt plus the expected response exceeds the context window, you will get an error. For most business documents this is not a problem, but long transcripts or large code files can push against limits.

Before building a feature, estimate your token usage. Multiply your average prompt length by your expected request volume and check the pricing page. Set a hard monthly spending cap in the dashboard to prevent runaway costs during development.

Rate Limits and Production Readiness

Rate limits exist at two levels: requests per minute (RPM) and tokens per minute (TPM). New accounts start at conservative limits and can request increases by providing usage details to OpenAI. Paid accounts with billing history qualify for higher tiers.

In production, always handle the 429 rate-limit error with exponential backoff: wait 1 second, retry; if it fails again, wait 2 seconds, then 4, then 8. Most client libraries include built-in retry logic you can enable with a single option.

Caching is equally important. If your application sends the same prompt repeatedly (a fixed system message plus a user question), cache the response for a short period. This reduces both cost and latency.

Integrating in a Real Application

A minimal integration follows three steps: read the user input, send it to the API with a system prompt that defines the behavior you want, and display or process the response. For most use cases, the system prompt does the heavy lifting. A well-written system prompt that describes the task, tone, and output format produces far better results than a bare user message.

For more complex pipelines, consider separating concerns: one function calls the API, another formats the prompt, another processes the response. This makes testing easier and lets you swap models without rewriting business logic.

Our tip: Start with a smaller, cheaper model like GPT-4o mini during development. It is faster and costs significantly less. Switch to a more capable model only for the specific steps where you actually need higher quality, and keep track of which steps those are.

Frequently asked questions

How much does the OpenAI API cost?

Pricing is per token and varies by model. Cheaper models like GPT-4o mini cost a fraction of GPT-4o. Input tokens and output tokens are billed separately, and output tokens are typically more expensive. You can estimate costs using OpenAI's tokenizer tool before you build. Always set a usage limit in your account dashboard to avoid unexpected bills.

What is the difference between the chat completions endpoint and the embeddings endpoint?

The chat completions endpoint generates text based on a conversation history. You send a list of messages and get a text response back. The embeddings endpoint converts text into a numerical vector that captures semantic meaning. Embeddings are used for search, similarity comparisons, and retrieval systems rather than text generation.

What are rate limits and how do they affect my application?

Rate limits cap how many requests and tokens you can send per minute and per day. New accounts start at low limits and can request increases. If your application exceeds a limit, the API returns a 429 error. Good integrations catch this error, wait briefly, and retry automatically. For high-volume use cases, caching repeated responses reduces how often you hit limits.

Want a dashboard or automation built for you?

Stuurboard BI builds custom Power BI dashboards and workflow automations. Pragmatic, fast, and without the fuss.