DEV Community

mogee
mogee

Posted on

I Built an Open-Source Bridge That Turns Claude Code Into a REST API

Why I Built This

If you're already paying for Claude Code (Max plan), you have access to an incredibly powerful AI right in your terminal. I wanted to get more out of that subscription — use it from my web apps, Slack bots, CI pipelines, and Python scripts running on other machines.

Anthropic does have an official API, but it's pay-per-token — a separate cost. Since I'm already paying for a subscription, I figured: why not use what I've got?

There are some great open-source wrappers like coder/agentapi and claude-code-api, but they're designed for local use only. I needed something that works remotely too — without having to set up reverse proxies, TLS certificates, and authentication from scratch.

What It Does

claude-api-bridge lets you use your existing Claude Code subscription as a REST API, accessible from anywhere.

One command. That's it.

npx claude-api-bridge start
Enter fullscreen mode Exit fullscreen mode

And you get:

✅  Claude Code CLI detected
🚀  API server running on http://localhost:3456
🔑  Admin Token: cab-a1b2c3d4e5f6...
🌐  Public URL: https://random-words.trycloudflare.com
Enter fullscreen mode Exit fullscreen mode

Now you have a real HTTPS endpoint that you can call from any app, anywhere in the world.

How It Works

Your App (anywhere on the internet)
    │
    │ HTTPS
    ▼
Cloudflare Tunnel (automatic SSL, zero config)
    │
    ▼
claude-api-bridge (your desktop)
    ├─ Token Authentication (SHA256)
    ├─ Request Queue
    └─ Claude Code CLI (your subscription)
Enter fullscreen mode Exit fullscreen mode

Your desktop becomes the API server. Claude Code runs locally using your existing subscription. Cloudflare Tunnel handles the networking — no port forwarding, no ngrok, no reverse proxy setup.

Key Features

Feature Details
🚀 One-command setup No config files, no environment variables
🌐 Remote access Cloudflare Tunnel with automatic HTTPS
🔑 Token management Create, revoke, expire. SHA256 hashed — never stored in plaintext
💬 Session modes Stateless (fresh each time) or Stateful (conversation memory)
📊 Web dashboard Monitor at /dashboard
🔒 Secure Rate limiting, CSP headers, execution timeout, output size limits
💾 Zero dependencies SQLite for storage — no Redis, no Postgres

Quick Example

Python

import requests, time

BASE = "https://your-url.trycloudflare.com"
TOKEN = "cab-your-token"
H = {"Authorization": f"Bearer {TOKEN}", "Content-Type": "application/json"}

def ask(msg):
    r = requests.post(f"{BASE}/api/ask", json={"message": msg}, headers=H)
    rid = r.json()["requestId"]
    while True:
        p = requests.get(f"{BASE}/api/ask/{rid}", headers=H).json()
        if p["status"] == "completed": return p["response"]
        time.sleep(2)

print(ask("Write a function to check if a number is prime"))
Enter fullscreen mode Exit fullscreen mode

JavaScript

async function askClaude(message) {
  const res = await fetch(`${BASE_URL}/api/ask`, {
    method: "POST",
    headers: { Authorization: `Bearer ${TOKEN}`, "Content-Type": "application/json" },
    body: JSON.stringify({ message }),
  });
  const { requestId } = await res.json();

  while (true) {
    const poll = await fetch(`${BASE_URL}/api/ask/${requestId}`, {
      headers: { Authorization: `Bearer ${TOKEN}` },
    });
    const data = await poll.json();
    if (data.status === "completed") return data.response;
    await new Promise(r => setTimeout(r, 2000));
  }
}
Enter fullscreen mode Exit fullscreen mode

How It Compares

Feature claude-api-bridge coder/agentapi claude-code-api
Remote access (HTTPS) ✅ Built-in ❌ Local only ❌ Local only
Token management ✅ Full CRUD ❌ None ⚠️ Static keys
Token security ✅ SHA256 hash ❌ N/A ❌ Plaintext
Session persistence ✅ SQLite ❌ In-memory ⚠️ In-memory
Crash recovery ✅ Auto ❌ No ❌ No
Zero config ✅ One command ✅ One command ⚠️ Config needed

Limitations (Being Honest)

  • Your desktop must be running — Claude CLI executes on your machine
  • One request at a time — Claude CLI is single-threaded, requests are queued
  • Free tunnel URL changes on restart — use a Cloudflare account for a fixed domain
  • Response time varies — typically 10 seconds to 2 minutes depending on the task

Try It

npx claude-api-bridge start
Enter fullscreen mode Exit fullscreen mode

GitHub: github.com/smy383/claude-api-bridge
npm: npmjs.com/package/claude-api-bridge

Star ⭐ if you find it useful!


Built by the team behind ttapp — the mobile remote for Claude Code.

Top comments (0)