From ChatGPT Web to the API: Your First Integration
The main change: you now control the application
Moving from ChatGPT web to an API is not simply replacing a browser tab with a script. The web product manages the interface, conversation storage, and available tools. With an API, your application decides what context to send, how to display responses, and what happens when a request fails.
The quickest migration is deliberately small: choose one task, send one request, inspect the result, then add conversation history and operational safeguards. Here is what changes and how to get started.
1. Separate your subscription from API access
A ChatGPT subscription does not automatically fund API usage. API credentials, billing, model availability, and limits belong to the provider serving your requests. A relay has its own account and balance, separate from your ChatGPT account.
59API is a low-cost, pay-as-you-go AI API relay offering GPT and Claude models, including Opus, Sonnet, and Haiku. Its service origin is https://api.59api.com. It supports OpenAI SDK integrations, Claude Code, and Codex, although each client still needs the appropriate endpoint and authentication configuration.
59API offers native, official-quality models without downgrading and is positioned among the cheapest relays. Compare the current price for your exact model before choosing: request size, output length, and any caching charges affect the final bill.
2. Configure credentials and the correct base URL
- Create a 59API account, obtain an API key, and check the available balance.
- Choose an exact model ID from its current model list. A display name such as “Sonnet” is not necessarily a valid API identifier.
- Store the key as the server-side environment variable OPENAI_API_KEY. Never embed it in browser JavaScript or commit it to Git.
- Set OPENAI_BASE_URL to 59API’s documented OpenAI-compatible API base. Start from https://api.59api.com and verify whether the documented path includes /v1; avoid missing or duplicating that segment.
Existing OpenAI SDK projects generally need a provider key, the compatible base URL, and a supported model ID. Compatibility makes migration easier, but does not mean every provider-specific feature behaves identically.
3. Send one minimal request
For a Chat Completions-compatible endpoint, replace REPLACE_WITH_MODEL_ID below with a listed model. With the environment variables configured, run this command in a terminal:
curl "$OPENAI_BASE_URL/chat/completions" -H "Authorization: Bearer $OPENAI_API_KEY" -H "Content-Type: application/json" -d '{"model":"REPLACE_WITH_MODEL_ID","messages":[{"role":"user","content":"Reply with a one-sentence greeting."}],"max_tokens":100}'
Inspect the returned content and usage fields. If authentication fails, check the key and authorization header. If you receive a model or route error, check the exact model ID and endpoint path before changing the prompt. Some newer models use different output-limit parameters, so follow the selected model’s documentation if max_tokens is rejected.
4. Rebuild context instead of expecting web memory
Your ChatGPT chats, saved memory, custom instructions, and uploaded files do not automatically accompany API calls. Send the instructions and relevant context explicitly.
- Instructions: supply your application’s rules using the instruction role supported by the selected API and model.
- Conversation: persist relevant user and assistant messages, then include them in subsequent requests.
- Documents: provide supported attachments or retrieve relevant excerpts yourself.
- Tools: implement and authorize tool execution; do not assume browser search or code execution is included.
Keep history bounded. Repeatedly sending an entire conversation increases input usage and can exceed the model’s context window. Summaries and selective retrieval are usually better than unlimited accumulation.
5. Add cost controls before production
Start with a cheaper model for routine tasks and escalate only when quality requires it. Cap output length, record actual usage, and test representative prompts rather than judging by one successful answer.
Add request timeouts, bounded retries with backoff for transient failures, and spending alerts. Review the relay’s data-handling terms before sending sensitive information. Keep API keys and confidential prompts out of logs.
If this setup fits your project, sign up for 59API and run a small, budget-capped trial. Its pay-as-you-go access makes model comparison practical, while its referral rebate offers an additional potential saving under the current program terms.
Ready to get started?
Connect Claude & GPT in minutes at the lowest prices — full-power, never downgraded. Sign up to get your API key.
Sign up free