---
title: "Call Claude or GPT from your app without exposing the API key"
url: https://while.sh/en/guides/call-llm-api-without-exposing-key/
author: "Aloïs Oddon, co-founder of Nextmap"
updated: 2026-11-19
---

# Call Claude, GPT or Mistral from your app without exposing the API key (2026)

Your app talks to an AI model, and its key costs a lot if it leaks. With while, your app calls Claude, GPT or Mistral without ever holding the key, every call is recorded, and your AI connects it all over MCP.

> **The short version.** Your app calls Claude, GPT or Mistral without holding the key, which is kept in while. Your AI connects it all over MCP and never sees it. What matters, like deleting a database, waits for a person's approval.

## Prerequisites

- An app, deployed on while or ready to be.
- The key from your AI provider: Anthropic, OpenAI or Mistral.
- An AI that works in a terminal: Claude Code, Codex, Cursor or Gemini CLI.

## Step 1: Give your AI this message

Open your AI in your app's folder, and paste this message into it:

```text titre="The message to give your AI"
Connect to while with my account (while.sh/agents), then put my app on while.
```

Your AI adds while's MCP server itself. If it asks you to, restart it. On the first call to while, a page opens in your browser. In Claude Code, if it doesn't open, type /mcp, choose while, then Authenticate. On that page, choose "A new app", then click Authorize.

## Step 2: Paste the key into while

Your AI declares the connection to Anthropic and gives you a link. You open it, paste the key and approve. Your AI never sees it.

## What your AI does

It adds the connection to the service, in while.yaml:

```yaml titre="while.yaml"
services:
  - name: web
    port: 3000
    connections: [anthropic]
```

Your app receives while's address for Anthropic in `ANTHROPIC_URL`, and calls the Messages API without a key (source 1):

```js titre="server.js"
const r = await fetch(`${process.env.ANTHROPIC_URL}/v1/messages`, {
  method: 'POST',
  headers: { 'content-type': 'application/json' },
  body: JSON.stringify({
    model: 'claude-sonnet-5-5',
    max_tokens: 1024,
    messages: [{ role: 'user', content: question }],
  }),
});
```

The key and the version header are added to each call, and every call is recorded. Calls to Claude go through from the first day, with no learning period.


<figure class="illus"><img src="/illustrations/guide-fonction-garde-1200.webp" srcset="/illustrations/guide-fonction-garde-768.webp 768w, /illustrations/guide-fonction-garde-1200.webp 1200w" sizes="(min-width: 48rem) 46rem, 100vw" width="1536" height="1024" loading="lazy" decoding="async" alt="Calling Claude, GPT or Mistral with while: credentials kept out of the AI's reach, every call logged in the history, a person's approval for what matters and a daily backup, in France" /><figcaption>Around every app, while keeps the credentials, the history, the approvals and the backups.</figcaption></figure>

## Switch models

> You, to your AI: Switch the summaries to Claude Haiku, so we pay less.

Your AI changes the model in your code and deploys again. The key stays as it is.

## Change the key

> You, to your AI: We need to change the Anthropic key.

You paste the new key into while, and your app uses it from the next call, with no new deployment.

## See your app's calls

> You, to your AI: Why did the summaries fail last night?

Your AI reads the latest call and the calls the connection refused in while, and fixes the problem.

## Add a second provider

> You, to your AI: Connect Mistral too, as a backup.

Your AI declares a second connection and adds it to while.yaml, you paste its key into while, and your app receives `MISTRAL_URL`. The Pro plan includes up to 25 API connections.

## Go back to the previous version

> You, to your AI: Go back to the previous version.

In the 30 minutes after a production release, the previous version comes back in a second. After that, it takes a few seconds. Once the previous version is back, your AI reads what broke in while's logs, fixes it and deploys again.

## Next steps

- [Connect Stripe or Brevo with no key in your code](/en/guides/api-keys-out-of-code/)
- [Deploy a Claude artifact](/en/guides/deploy-claude-artifact/)
- [Security on while](/en/security/)

## Sources

1. [Anthropic, the Messages API](https://platform.claude.com/docs/en/api/messages), 2026-11-02

## Questions

### Can my AI see the key?

No. You enter it in while, never in your AI or in your code, and it is added to each call.

### Which providers work?

Anthropic, OpenAI and Mistral, and any other HTTP API with its key.

### Do I need to change my code?

Barely: your app calls the address it receives in ANTHROPIC_URL instead of Anthropic's, and it no longer sends a key.

### What if someone steals my app's code?

The key isn't in it: they can't use it anywhere else.

### Where do my app and its data run?

In France, at Nextmap. The database is backed up every day, with a way back over 14 days, once a person approves.
