Claude API in a Chrome Extension: API Key Setup
If you're building a Chrome extension that calls Claude, the first question is almost always "where do I put my API key?" The short answer: not directly in the extension's JavaScript. Anthropic's API doesn't allow browser-based requests with a raw key exposed in client-side code, and even if it did, shipping a secret inside a .crx file means anyone can extract it in seconds with the Chrome DevTools or by unpacking the extension folder.
This guide covers the two things developers actually search for when they hit this problem: how to get and configure a Claude API key for extension use, and how to structure the extension so that key never ends up in the browser where users (or malicious actors) can read it.
Why you can't just call the Claude API from a content script
Manifest V3 extensions run JavaScript in three places: content scripts (injected into web pages), the background service worker, and extension pages (popup, options). All three ship as plain, inspectable files. Anyone who installs your extension can open chrome://extensions, enable Developer Mode, and read every line of code — including any string that looks like an API key.
There's also a functional problem, separate from security: Anthropic's API is designed to be called from a trusted server environment, not a browser tab. Making authenticated calls directly from extension code means your key travels with every installed copy of the extension. If one user's machine is compromised, your entire API budget is at risk — rate limits, cost overruns, and potential bans for abuse all become your problem, not theirs.
The fix is the same pattern used for any client-facing app: put a thin backend between the extension and the model provider, and let the extension talk to that backend instead.
The architecture that actually works
Chrome Extension (popup/background) → Your backend/proxy → Claude API
The extension never holds a long-lived secret with full account access. Instead, it either:
- Sends requests to a backend you control, which holds the real Claude API key server-side, or
- Uses a scoped, per-application key designed for exactly this situation.
This is where SubToAPI fits in. It turns your existing Claude access into an HTTPS API with its own application keys (sub_live_...) that are separate from your Anthropic account credentials. You generate a key scoped to one app — in this case, your Chrome extension — set it as the bearer token your extension sends, and revoke it independently if it ever leaks, without touching your main account. Usage per key shows up in the dashboard, so you can see exactly how much a specific extension install is consuming.
Setting up the key in your extension
Store the key server-side if you have a backend, or have users paste their own key into the extension's options page if you're distributing an open-source or BYO-key tool. Either way, keep it out of content scripts and out of anything injected into web pages.
A typical background service worker call looks like this:
// background.js (service worker)
async function askClaude(prompt) {
const response = await fetch("https://api.subtoapi.app/v1/messages", {
method: "POST",
headers: {
"Authorization": `Bearer ${SUBTOAPI_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
model: "claude-3-5-sonnet-latest",
max_tokens: 1024,
messages: [{ role: "user", content: prompt }]
})
});
return response.json();
}
Key points for Manifest V3:
- Make the call from the background service worker, not a content script. Content scripts share the page's execution context and are more exposed to inspection by other scripts on that page.
- If you let end users supply their own key, save it with
chrome.storage.local, notlocalStorage. Extension storage is sandboxed per-extension and isn't accessible to the web pages the user visits. - Add the API host to
host_permissionsin your manifest so the fetch isn't blocked:
{
"host_permissions": ["https://api.subtoapi.app/*"]
}
- Never log the key to the console in production builds — DevTools output is visible to anyone debugging the extension.
Handling streaming responses in a popup
Chrome extension popups close when the user clicks away, which breaks long-running streaming connections. If your extension needs streaming output (useful for showing Claude's response token-by-token in a side panel or popup), run the fetch in the background service worker and pass chunks to the UI with chrome.runtime.sendMessage, rather than streaming directly into the popup's own script. See /docs/streaming for details on how streamed responses are formatted.
// background.js
const res = await fetch("https://api.subtoapi.app/v1/messages", {
method: "POST",
headers: { "Authorization": `Bearer ${SUBTOAPI_KEY}`, "Content-Type": "application/json" },
body: JSON.stringify({ model: "claude-3-5-sonnet-latest", stream: true, max_tokens: 1024, messages })
});
const reader = res.body.getReader();
const decoder = new TextDecoder();
while (true) {
const { done, value } = await reader.read();
if (done) break;
chrome.runtime.sendMessage({ type: "chunk", text: decoder.decode(value) });
}
If you're distributing the extension publicly
For extensions you plan to publish on the Chrome Web Store, don't bundle any key — app-specific or otherwise — in the package. Instead:
- Have your backend hold the credential and authenticate users (OAuth, license key, or simple email/password) before proxying requests to Claude.
- Or let users bring their own key via an options page, clearly explaining where it's stored and that it's never transmitted anywhere except the API endpoint they configure.
SubToAPI supports the first pattern well for teams shipping a paid extension: one account creates per-user or per-feature keys under /pricing plans starting at Solo (€9), with Team and Scale tiers adding seats for multiple developers managing keys from a shared dashboard. You get the usage metadata and team structure without building your own billing and key-management layer.
Getting started is a five-minute setup: sign up at /signup, grab a key from the dashboard, and follow /docs/quickstart to send your first request before wiring it into the extension's manifest and background script.
questions
Can I put my Anthropic API key directly in a Chrome extension? Technically you can paste it into code, but it's readable by anyone who inspects the extension's files, and it isn't scoped — a leak exposes your whole account. Use a backend proxy or a scoped application key instead.
Why does my fetch to the Claude API fail from a content script? Content scripts run in the page's context and are more exposed than the background service worker. Move API calls to background.js, add the API host to host_permissions in your manifest, and make the request from there.
How do I handle streaming Claude responses in a popup that might close? Run the streaming fetch in the background service worker, not the popup, and forward chunks to the popup UI with chrome.runtime.sendMessage so the connection survives the popup closing. See /docs/streaming for message formatting.