Claude API Browser Extension: Build Tutorial
Building a browser extension that calls Claude usually means solving three problems at once: the Manifest V3 security model, where to make the actual API request from, and how to avoid shipping a raw API key inside a public extension bundle. This tutorial walks through all three, with a working manifest, background worker, and popup UI you can adapt for summarizers, writing assistants, page Q&A tools, or research copilots.
The short answer for intent-matching purposes: you build the extension like any Manifest V3 extension (background service worker + content script + popup), but you do not call the Claude API directly from extension code with a hardcoded key — you proxy the call through a backend endpoint, because anything bundled in a Chrome extension is extractable by users. The rest of this guide shows exactly how to structure that.
Why you can't just fetch() Claude from your extension
Chrome extensions are distributed as unpacked, inspectable code. If you embed an API key in background.js, anyone who installs your extension can open DevTools, read the key, and use your Claude quota. This is true even with obfuscation — it's a packaging format, not encryption.
There are two viable architectures:
- User brings their own key — the user pastes their own API key into the extension's options page, stored in
chrome.storage.local. Fine for personal tools, bad for anything you want to distribute or monetize. - You run a backend that holds the real credential — the extension calls your backend, your backend calls Claude (or a wrapper like SubToAPI) with a server-side secret. This is the only safe option if you're shipping the extension publicly or charging for it.
SubToAPI fits option 2 well because it gives you scoped sub_live_... application keys instead of your raw Anthropic credential — if an extension version somehow leaks a key, you revoke that one key from the dashboard without touching your main account. See /docs/quickstart for setup.
Step 1: the manifest
A minimal Manifest V3 setup for a Claude-powered extension:
{
"manifest_version": 3,
"name": "Page Explainer",
"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"]
}
]
}
host_permissions must explicitly list the API domain you're calling — Chrome will block cross-origin fetches from the service worker otherwise.
Step 2: content script grabs page context
// content.js
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
if (msg.type === "GET_PAGE_TEXT") {
sendResponse({ text: document.body.innerText.slice(0, 8000) });
}
});
Truncate aggressively. Full page dumps blow through context windows fast and cost money for no benefit — most explainer/summarizer use cases don't need the whole DOM.
Step 3: background worker calls your backend
This is where the Claude call actually happens, but routed through your own server instead of embedding a key:
// background.js
chrome.action.onClicked.addListener(async (tab) => {
const [{ result }] = await chrome.scripting.executeScript({
target: { tabId: tab.id },
func: () => document.body.innerText.slice(0, 8000)
});
const res = await fetch("https://your-backend.com/explain", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ text: result })
});
const data = await res.json();
chrome.storage.local.set({ lastExplanation: data.explanation });
});
Your backend (a small Node/Express or serverless function) is the only thing that holds credentials:
// your-backend.com/explain
app.post("/explain", async (req, res) => {
const response = await fetch("https://api.subtoapi.app/v1/messages", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.SUBTOAPI_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
model: "claude-sonnet-4",
max_tokens: 500,
messages: [
{ role: "user", content: `Explain this page in plain language:\n\n${req.body.text}` }
]
})
});
const data = await response.json();
res.json({ explanation: data.content[0].text });
});
Full request/response shape is in /docs/messages.
Step 4: streaming into the popup
For a more responsive UX, stream tokens from your backend to the popup via Server-Sent Events or a simple chunked fetch, and relay through chrome.runtime.sendMessage to update the popup DOM as chunks arrive. SubToAPI supports streaming on the same endpoint — see /docs/streaming for the SSE format if you want to pipe it through with minimal buffering on your backend.
Step 5: adding tool use for richer extensions
If your extension needs Claude to take structured actions — extracting structured data from a page, filling a form, triggering a lookup — use tool definitions rather than parsing free text. Define a tool schema on your backend request and handle the tool_use block in the response before relaying a final answer to the extension. Details and examples are in /docs/tools.
Step 6: packaging and store review
Zip the extension directory (manifest, scripts, icons, popup HTML/CSS) and upload through the Chrome Developer Dashboard. Two review gotchas specific to AI extensions:
- Disclose data sent off-device. Page text is being sent to a third-party API — say so in your privacy policy and in the extension's permissions justification field.
- Remote code execution flags. Don't eval() model output or inject unsanitized HTML from a Claude response into the page; render as text or sanitize first.
Keeping costs and keys sane
Once the extension has real users, track usage per install if possible (even a rough count) so you can see which features drive token consumption. If you're running the backend through SubToAPI, usage metadata is visible per key in the dashboard, and you can issue separate keys for staging vs. production builds of the extension, or per teammate if multiple people are iterating on it — see /pricing for seat-based plans if you're building this with a team.
questions
Can I call the Claude API directly from a browser extension without a backend? Technically yes with host_permissions, but only safely if each user supplies their own API key stored locally. Shipping a shared key inside the extension bundle is extractable by any user and will get your key abused.
Does Manifest V3 block API calls to external domains? No, but you must declare the domain under host_permissions in the manifest, and calls must happen from the background service worker or a context with proper CORS headers on the response.
How do I stream Claude responses into a popup UI? Stream from your backend to the extension using SSE or chunked fetch, then forward each chunk through chrome.runtime.sendMessage to update the popup incrementally — see /docs/streaming for the response chunk format.