How do I restrict an API key to specific AI models?
Create one key per app or environment in Dongee AI Router, restrict each key to the models it needs, keep it in server environment variables and review its usage to limit leaks and unexpected spending.
To restrict an API key to specific models with Dongee AI Router, a unified, OpenAI-compatible API for multiple AI models, create a separate key for each app or environment and, in your client area, limit it to the models that app uses. Store the key in server environment variables, never in the browser.
Then review each key's usage. This limits the damage if a key leaks and gives you better control over spending.
Why you should limit each key to specific models
A key with no restrictions gives access to every available model. If someone gets hold of it, or if a bug in your code sends requests to a model you didn't expect, the usage comes out of your balance. When you limit the key to the models you need, that risk shrinks to a small, known set of models.
Dongee AI Router is a unified API for multiple AI models. It prioritizes fast models hosted in Dongee's data center in Canada, works on a prepaid balance starting at 3 USD and has no monthly fee. It follows the OpenAI standard and lets you create as many keys as you want per account, each one limited to the models you choose.
Step by step: how to restrict an API key to specific models
- Activate AI Router and add credit. You activate it from your Dongee client area (you need to be a Dongee customer). The balance is prepaid starting at 3 USD, and new accounts receive welcome credit.
- Create one key per application or environment. For example, one for the production chatbot, another for the same chatbot in testing and another for an n8n workflow. Since there is no limit on keys per account, you don't have to share one key across projects.
- Restrict each key to the models it needs. The list of available models is in your client area. Select only the ones that app uses. If your test environment uses a fast, low-cost model, don't give it access to the others.
- Store the key in server environment variables. Don't put it in your source code, in a GitHub repository or in JavaScript that runs in the browser. If the key reaches the browser, anyone can see it and spend your balance.
- Change the base URL and the key in your code. Because AI Router is OpenAI-compatible, you only change those two values. Copy the base URL shown in your client area.
- Review usage per key. In the client area, you can see usage by model and by key, plus the details of each request. If a key spends more than usual, you know which app is causing it.
Node.js example with the OpenAI SDK
The names AI_ROUTER_BASE_URL and AI_ROUTER_API_KEY are just examples, and you can use any names you like. Put the base URL shown in your client area in the first one and that app's restricted key in the second.
import OpenAI from "openai";
const client = new OpenAI({ baseURL: process.env.AI_ROUTER_BASE_URL, apiKey: process.env.AI_ROUTER_API_KEY });
const response = await client.chat.completions.create({ model: "ALLOWED_MODEL", messages: [{ role: "user", content: "Hello" }] });
console.log(response.choices[0].message.content);
Replace ALLOWED_MODEL with one of the models from your client area list that you enabled for that key. This code must run on your server, for example in a Node.js API, and never in the browser. AI Router also supports streaming if you want to show the response as it is generated.
With and without a per-model limit
| Situation | Key with no per-model limit | Key limited to specific models |
|---|---|---|
| The key leaks | Whoever has it can use any model with your balance | They can only use the models you enabled |
| A bug in the code requests another model | The request goes through and uses your balance | The key doesn't work for models you didn't authorize |
| Spending analysis | It's hard to tell which app used what | With one key per app, the per-key usage shows it clearly |
| Rotating or revoking the key | Affects every app that shares it | Affects only one app or environment |
When AI Router is a good fit and when it isn't
It's a good fit if you want a single OpenAI-compatible API, want to pay only for what you use with no monthly fee and want to control spending with several restricted keys. It also works if you already use your own gateway, such as LiteLLM, because AI Router can sit behind it.
It's not a good fit in these cases:
- You need a model that isn't on the list in your client area.
- You expect the provider to switch models automatically when one fails. Dongee doesn't provide its own fallback, so you program the routing yourself. If this topic interests you, read this guide on smart routing between language models.
- Your app has no backend, and you want to call the API directly from the browser. You need a server layer first.
- You don't want to be a Dongee customer, which is required to activate it.
Frequently asked questions
How many API keys can I create?
As many as you want, because there is no limit on keys per account. We recommend one per application and per environment.
Can I use the key in the frontend if I limit it to a single model?
We don't recommend it. Even when the key is restricted, anyone who sees it could spend your balance on that model. Keep it on the server and have your frontend call your own backend.
What happens if my balance runs out?
Requests stop, and you don't go into debt. Check usage per key so you can top up in time.
Does Dongee store the content of my conversations?
No. AI Router only records the billing data for each request, never the content.
Does limiting models also help me use fewer tokens?
It helps you control which models are used, but your savings also depend on your prompts and how you route requests. Learn more in this guide to optimizing token usage with AI routers.
In summary
Use one key per app or environment, limit each key to the models it needs, keep it on the server and review its usage. These four habits reduce the risk of leaks and unexpected costs. To round out your project's security, see how to keep an AI-built app secure and online.
Comenta lo que quieras
Unete a la charla
Solo ingresa tu email