Base URL and SDK MigrationNovember 6, 2026Flatkey Team

ChatGPT API: how to get access, integrate, and control costs

A Portuguese guide to ChatGPT API access, keys, first integration, costs, and the difference between direct calls and a compatible gateway.

ChatGPT API: how to get access, integrate, and control costs

Those looking for the ChatGPT API generally want to put AI responses into an application, bot, or internal workflow. The first detail is to distinguish the products: ChatGPT is the ready-made interface for chatting; the OpenAI API is the service for developers to send requests to models. A ChatGPT subscription is not, by itself, an API key. You can use the API directly through OpenAI or evaluate a compatible gateway, such as Flatkey, when you need to access multiple models through a single integration.

Quick answer: create an API account, configure billing and limits with your chosen provider, generate a key with minimum access, store it on the server, and make a small call. Then validate the response, usage, and errors before opening up traffic. Do not put the key in the browser, a repository, or a mobile application distributed to users.

ChatGPT and API: what is the difference?

Item ChatGPT OpenAI API or compatible gateway
Main use Chat in the OpenAI interface Integrate models into your software
Access ChatGPT account and plan API credential and project or billing account
Billing App plan and features Usage based on the model and features used
Application control Interface settings Code, prompts, authentication, logs, and limits under your responsibility

The OpenAI help center confirms that ChatGPT billing and API platform billing are separate. Promotional credits may be available for eligible accounts, but they do not turn a subscription into unlimited API usage. Always check your organization's current terms before promising users a free quota.

Technically, there is no generic endpoint called the “ChatGPT API” that replicates the website interface. In everyday language, the term usually means “using OpenAI models via API.” This matters when you look for documentation: choose the API reference and the model appropriate for the task, rather than a tutorial that tries to automate the web interface.

How to obtain a key securely

  1. Create or access an account on the OpenAI API platform for direct use. Check the project, payment method, and limits available for it.
  2. Create a key for the specific application or environment. Do not share a single credential between development, staging, and production if you need to separate usage and incidents.
  3. Store the key in a secrets manager or server environment variable. Avoid pasting it into code, screenshots, or analytics tools that store prompts.
  4. Make a test call with data containing no sensitive information. Check the HTTP status, model identifier, and reported usage.
  5. Set a budget, alerts, and a way to revoke the credential. Test rotation before relying on it in production.

If you choose a compatible gateway, the key is issued by that gateway and billing follows its rules. Do not reuse an OpenAI key as if it were a Flatkey key. The official Flatkey compatibility page documents the base URL https://router.flatkey.ai/v1 and a chat completions example using the OpenAI SDK. Check the catalog for the model identifier enabled for your account before sending real traffic.

First call: directly through OpenAI

OpenAI's current documentation presents the Responses API as the path for generating text in new applications. The example below is only a connectivity test. Replace the identifier with the model available in your project and check the current reference before taking the snippet to production.

import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
response = client.responses.create(
    model="gpt-6-luna",
    input="Responda em uma frase: o que é uma API?",
)
print(response.output_text)

The result needs to be evaluated by your application. A successful HTTP response does not guarantee that the text is correct, safe, or in the expected format. For structured data, validate the schema; for factual answers, validate the sources and behavior in error cases.

Alternative: Flatkey-compatible client

If the requirement is to switch between models through the same integration, Flatkey publishes an interface compatible with the OpenAI API for chat completions. Here, the credential is FLATKEY_API_KEY, and the model identifier must be a current alias from the Flatkey catalog. This snippet is intentionally different from the Responses API example above: we do not assume that all endpoints or features from all providers are identical.

import os
from openai import OpenAI

client = OpenAI(
    base_url="https://router.flatkey.ai/v1",
    api_key=os.environ["FLATKEY_API_KEY"],
)
response = client.chat.completions.create(
    model=os.environ["FLATKEY_MODEL"],
    messages=[{"role": "user", "content": "Explain an API in one sentence."}],
)
print(response.choices[0].message.content)

Check the alias, applicable region, price, and availability in the Flatkey documentation and catalog. Do not merely change the URL and conclude that streaming behavior, tools, limits, or billing will be the same: perform contract and load tests for the feature you actually use.

How much does it cost to use the ChatGPT API?

API cost depends on the model, input and output tokens, and any additional features the product charges for. The basic formula for text is:

cost ≈ (input tokens ÷ 1,000,000 × input rate) + (output tokens ÷ 1,000,000 × output rate) + additional features

Use the current OpenAI rates for direct calls, or the Flatkey pricing page for calls billed by Flatkey. Do not mix the two tables. Display the cost per completed task in your dashboard, because a long response, an automatic repetition, and an error with a retry can change spending per user even when the price per million tokens does not change.

Take three simple measurements before estimating a monthly budget: typical prompt size, the response size actually needed, and the number of calls per task. Run a small real-world sample, calculate median and consumption percentiles, then project with a margin for peaks. Set an output cap and avoid sending the entire conversation history with every call unnecessarily.

Errors and limits that appear first

  • 401/403: missing, revoked, incorrect-product, or unauthorized key. Confirm the host, project, and credential; do not expose the key in logs.
  • 429: request, token, or credit limit. Read the returned code and headers, apply progressive backoff, and limit retries. Mass retries can increase both the queue and the bill.
  • 400: incompatible model or field, malformed payload, or input over the limit. Compare the sent body with the endpoint reference and log a sanitized version for debugging.
  • Timeout: the server may have processed part of the work even if the client did not receive a response. Define timeouts and a retry policy by operation type.

When you use a gateway, distinguish between client, gateway, and upstream provider errors. Store a request identifier and a safe model/endpoint summary at every stage. The Flatkey client migration guide shows why limits and retries need to be reviewed after changing the base URL.

Checklist before going to production

  • Does the selected model meet the task's quality, language, and latency requirements?
  • Is the key restricted to the correct environment and kept out of the public client?
  • Does the application validate the response and handle empty or unexpected outputs?
  • Does the data sent to the API comply with your team's privacy policy?
  • Are there a budget, alerts, an output limit, and usage logging per task?
  • Does the retry policy prevent request storms and duplicate payments?
  • Is there fallback testing and a way to turn off the feature without interrupting the product?

Frequently asked questions

Do I need to pay for ChatGPT Plus to use the API?

No. The ChatGPT application subscription and API billing are separate. Check the API account billing dashboard and any promotional benefit applicable to your case.

Can I use the same OpenAI key on Flatkey?

No. To use the Flatkey route, create a Flatkey key. To call OpenAI directly, use the key issued by OpenAI. The SDK may be similar, but the key owner and billing change.

Is the API free?

Do not assume a permanent free tier. Promotional credits and limits change; check the current balance, project, and pricing page for the chosen service. Estimate the cost using a sample of your own workload.

Next step: make a small call to the chosen service, record the model, usage, time, and result, and only then scale up traffic. If you need to compare multiple providers, start with the Flatkey-compatible interface and validate each model before migrating the application.