Claude API Chrome Extension Development Guide
If you're building a Chrome extension that calls the Claude API — a writing assistant, a page summarizer, a research sidebar — you'll hit three problems almost immediately: CORS restrictions on direct API calls from content scripts, Manifest V3's stricter execution model, and the fact that you can't safely ship a Claude API key inside a browser extension that anyone can unpack and read.
This guide covers the actual architecture that works: where API calls should run inside your extension, how to structure a Manifest V3 service worker to talk to Claude, how to stream responses back into the page UI, and how to keep your API key out of the client bundle entirely.
Why you can't just call the Claude API from a content script
Content scripts run in the context of the web page, which means any fetch() call to an external API is subject to the page's CORS policy and visible to anyone inspecting network traffic on that tab. Even if the call succeeds, your API key is sitting in plaintext inside a script that ships with the extension — extracting it takes about thirty seconds with the Chrome DevTools or by unzipping the .crx file.
The fix is always the same: isolate API calls to the background service worker, and never hardcode a long-lived key in code that ships to users. For anything beyond a personal prototype, you also want a server-side layer between the extension and the model provider so you can rotate keys, meter usage per install, and revoke access without pushing a new extension version.
Extension architecture that works
A Claude-powered Chrome extension typically has three layers:
- Content script — reads page content, injects UI, sends messages to the background worker
- Background service worker — the only place that makes network requests, holds short-lived credentials
- Backend/API proxy — issues scoped keys, handles auth, forwards requests to Claude
For layer 3, you can run your own proxy server, or use a service like SubToAPI that turns your Claude access into a standard HTTPS API with its own sub_live_... application keys — so the key living in your extension's storage is scoped to your app, not your root Anthropic credentials, and can be revoked independently.
Setting up the manifest
Manifest V3 requires you to declare host permissions explicitly and run network calls from the service worker, not the page context:
{
"manifest_version": 3,
"name": "Claude Page Assistant",
"version": "1.0.0",
"permissions": ["storage", "activeTab", "scripting"],
"host_permissions": ["https://api.subtoapi.app/*"],
"background": {
"service_worker": "background.js"
},
"action": {
"default_popup": "popup.html"
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["content.js"]
}
]
}
Only declare host_permissions for the domains you actually call. This keeps the permission prompt users see at install time minimal and avoids review friction in the Chrome Web Store.
Background service worker: the only place that talks to the API
// background.js
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.type !== "ASK_CLAUDE") return;
(async () => {
const { apiKey } = await chrome.storage.local.get("apiKey");
const response = await fetch("https://api.subtoapi.app/v1/messages", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${apiKey}`
},
body: JSON.stringify({
model: "claude-sonnet-4",
max_tokens: 1024,
messages: [{ role: "user", content: message.prompt }]
})
});
const data = await response.json();
sendResponse({ text: data.content[0].text });
})();
return true; // keep the message channel open for async response
});
The content script sends a message, the service worker handles the network call, and the result goes back through sendResponse. This pattern keeps all credentials and network logic out of page-context code. Full request/response shapes are in the docs.
Streaming responses into the popup or sidebar
Users expect token-by-token output, not a spinner. Service workers can read streamed responses, but passing a live stream back to a content script or popup requires chunked messaging since chrome.runtime.sendMessage doesn't support streaming natively. The common pattern is to use a long-lived port:
// background.js
chrome.runtime.onConnect.addListener((port) => {
port.onMessage.addListener(async (msg) => {
const { apiKey } = await chrome.storage.local.get("apiKey");
const res = await fetch("https://api.subtoapi.app/v1/messages", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${apiKey}`
},
body: JSON.stringify({
model: "claude-sonnet-4",
max_tokens: 1024,
stream: true,
messages: [{ role: "user", content: msg.prompt }]
})
});
const reader = res.body.getReader();
const decoder = new TextDecoder();
while (true) {
const { done, value } = await reader.read();
if (done) break;
port.postMessage({ chunk: decoder.decode(value) });
}
port.postMessage({ done: true });
});
});
On the UI side, open a port with chrome.runtime.connect() and append each chunk as it arrives. Details on event formats and parsing SSE chunks are in the streaming docs.
Handling API keys safely in an extension
Never bundle a raw Claude API key in your extension code. Instead:
- Let users paste their own key into
chrome.storage.localvia the extension options page, or - Have your extension authenticate against your own backend, which issues a short-lived, scoped key per install
- If you're distributing the extension to many users under one plan, a per-app key model (like SubToAPI's
sub_live_...keys) lets you track usage per install and revoke access without republishing the extension — see pricing for seat-based options if you're shipping this to a team - Set
"storage"permission narrowly and never log keys toconsolein production builds, since extension console output is visible to anyone with DevTools open
Tool use and page-aware actions
If your extension needs Claude to take structured actions — extract structured data from a page, fill a form, trigger a browser action — use tool calling rather than parsing free text. Define a tool schema, pass the page DOM snippet as context, and let Claude return a structured tool_use block your content script can execute directly. See the tools documentation for schema examples.
Testing and debugging
- Load the unpacked extension via
chrome://extensionswith Developer Mode enabled and reload after every background script change - Use
chrome://extensions→ "service worker" link to inspect background console output directly, since it doesn't appear in the page's DevTools - Test with
host_permissionsrestricted to your exact API domain before submitting to the Chrome Web Store — overly broad permissions are a common rejection reason - Rate-limit requests client-side; a page-wide extension making one call per DOM mutation can burn through your quota fast
A quickstart for getting a working Claude integration running in minutes, independent of the browser extension wrapper, is available at /docs/quickstart, and you can get test keys by signing up at /signup.
questions
Can a Chrome extension call the Claude API directly from a content script? Technically yes, but it exposes your API key to anyone inspecting the page and is subject to CORS restrictions. Route all API calls through the background service worker instead.
How do I stream Claude responses into an extension popup? Use a long-lived chrome.runtime.connect() port between the popup/content script and the background worker, and forward decoded stream chunks through port.postMessage() as they arrive.
What permissions does a Claude-powered extension need in Manifest V3? Only host_permissions for your specific API domain, plus storage for saving keys and activeTab/scripting if you need to read or modify page content — avoid <all_urls> unless the extension genuinely runs everywhere.