Using an OpenAI-Compatible API Key in Your App
By Darus S Alfatah · 2025-08-14 · Tutorial · 10 menit
Author note: The step that most often breaks is forgetting to change base_url. Check those two lines first.
Focus of this article
This article explains the OpenAI-compatibility concept (base_url + API key) in a language-agnostic way. For a complete Python code walkthrough, see the companion article.
Read also: Python + AI API: A Step-by-Step Tutorial
Why Compatibility Matters
The biggest advantage of an all-in-one API key is compatibility with the OpenAI API. Every line of code you already wrote for OpenAI keeps working. Change two settings — base URL and API key — and your app can reach a dozen models.
Compatibility matters because the OpenAI SDK became an industry standard. Thousands of libraries and tools are built on it, so a compatible gateway unlocks all of them without rewriting anything.
Python Integration
from openai import OpenAI
client = OpenAI(
base_url="https://bankai.web.id/api",
api_key="sk-bankai-xxxxxxxx"
)
response = client.chat.completions.create(
model="auto",
messages=[{"role": "user", "content": "Explain how LLMs work"}],
temperature=0.7,
max_tokens=1000
)
print(response.choices[0].message.content)
Node.js Integration
import OpenAI from 'openai';
const client = new OpenAI({
baseURL: 'https://bankai.web.id/api',
apiKey: process.env.BANKAI_API_KEY
});
Streaming for Better UX
Streaming makes apps feel far more responsive: show partial text as it arrives instead of waiting for the full answer.
const stream = await client.chat.completions.create({
model: 'auto', messages, stream: true, max_tokens: 800
});
for await (const chunk of stream) {
const token = chunk.choices[0]?.delta?.content;
if (token) process.stdout.write(token);
}
Function Calling
Function calling lets the model request that your app run a specific function — check stock, create a ticket. You define the functions and schemas, the model chooses one and fills parameters, and your app executes. The model never touches the database directly, which is essential for security.
Safe practice: validate every parameter against a schema, limit available functions per user role, and never put credentials in function descriptions, because descriptions are sent to the model.
Handling Errors
Calls fail for many reasons: network issues, exhausted quota, unavailable model, or oversized input. Production code must distinguish them. 4xx usually means your request is wrong and retrying will not help; 5xx and timeouts deserve retries with increasing delay.
Use exponential backoff with a cap — three attempts at 1, 2, and 4 seconds. Never retry indefinitely; that only inflates the bill and worsens the outage.
Check Compatibility Before Migrating
Test three things: error response shape, support for the parameters you use (such as response_format or tools), and behaviour when a model refuses. These are the usual sources of subtle differences between providers.
Keeping Keys Safe
Never hardcode keys in scripts or frontend code. Use environment variables or a .env file excluded from version control, and rotate the key from the dashboard if it leaks.
Conclusion
Because the interface is standard, switching models is a configuration change rather than a project. That freedom is the real value: it lets you pick the cheapest model that meets your quality bar for each feature.
Checklist Before Going Live
- Key stored in environment variables, never in code or the frontend bundle.
- Timeouts and retries configured with a cap.
- Token usage logged per feature.
- A fixed test set run after every prompt change.
- A fallback model configured for outages.
Run through this list once per feature. Each item is cheap to do now and expensive to fix later, especially key exposure and unlimited retries.
A three-line migration example: An OpenAI client usually just needs base_url changed to the gateway endpoint and your api_key. The model name can stay the same or switch to another in the catalogue.
Frequently Asked Questions
Can beginners follow this tutorial?
Yes. The steps are explained in order with code examples, suitable for beginners and experienced developers alike.
Is BankAI’s endpoint compatible with the library I already use?
Yes, BankAI is 100% OpenAI-compatible, so libraries such as the OpenAI SDK and LangChain work without code changes beyond the Base URL and API key.
Where do I get an API key to follow this tutorial?
You get the API key (and Secret Token) after registering and completing payment in the BankAI dashboard.