Bridge by Wekoya / Guides / How to call an AI API from a mobile app without exposing your key

How to call an AI API from a mobile app without exposing your key

In short: Never put your API key in a mobile or web app: anyone can pull it out of the app and spend your balance. Either send requests through your own server, or have your server make a short-lived guest key for each signed-in user. On Bridge by Wekoya, a guest key expires within a day and stops at a naira limit you set.

Updated

Why a key in the app is not safe

Everything in an app you ship can be read by whoever installs it: the files, the network traffic, the strings in the code. Hiding or scrambling a key slows that down but does not stop it. A stolen key keeps working until you notice and revoke it.

Option 1: send requests through your server

The app calls your server, your server checks who the user is and calls the AI with your key, and passes the answer back. Your key never leaves your server. On Bridge, name the user on each request with the X-Bridge-End-User header and give each user a budget, so one user cannot spend everything. See your users' budgets.

Option 2: give the app a short-lived guest key

When the app needs to call AI directly (to stream a reply, or for live voice), your server makes a guest key for the signed-in user, and the app uses it like an API key. Each guest key:

Make a guest key on your server

// A route your signed-in users call. WEKOYA_API_KEY stays on the server.
app.post("/api/ai-key", requireSignedIn, async (req, res) => {
  const response = await fetch("https://wekoya-api.wekoya.tech/v1/guest-keys", {
    method: "POST",
    headers: {
      Authorization: `Bearer ${process.env.WEKOYA_API_KEY}`,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({ expires_in: 900, max_spend: 50, uses: ["chat"], end_user: req.user.id }),
  })
  const guest = await response.json()
  if (!response.ok) return res.status(502).json({ error: "Could not get a key" })
  res.json({ key: guest.key, expires_at: guest.expires_at })
})

Use it in the app

// React Native or the browser: ask your server for a key, then call Bridge with it.
const { key } = await fetch("https://your-server.example/api/ai-key", {
  method: "POST",
  headers: { Authorization: `Bearer ${userSessionToken}` },
}).then((r) => r.json())

const reply = await fetch("https://wekoya-api.wekoya.tech/v1/chat/completions", {
  method: "POST",
  headers: { Authorization: `Bearer ${key}`, "Content-Type": "application/json" },
  body: JSON.stringify({
    model: "gemini-3.8-flash",
    messages: [{ role: "user", content: "Suggest a name for my bakery" }],
  }),
}).then((r) => r.json())

When the guest key expires or reaches its limit, the app asks your server for a new one. The full reference, including live voice from a browser, is in browser and mobile apps.

Create a free account

Common questions

Can I hide my API key in a mobile app?

Not safely. Anything shipped in an app can be pulled out of it. Keep the key on your server, and give the app either answers from your server or a short-lived, limited guest key.

What happens if someone copies a guest key?

They can only use it until it expires, up to its naira limit, for the uses you allowed. On the web it also only works from your own web addresses. You can revoke it at any time.

Does this work with Flutter, Swift or Kotlin?

Yes. A guest key is used exactly like an API key, in the Authorization header of an ordinary HTTPS request, so any language or OpenAI-compatible library works.

Read next