How to Build a Local AI API Gateway with CLIProxyAPI in 15 Minutes

1 views 0 likes 0 comments 16 minutesOriginalTutorial

A practical guide to setting up CLIProxyAPI locally, unifying multiple AI providers under a single OpenAI-compatible API, and integrating it seamlessly with IDEs like Cursor.

#AI Tools # API Proxy # Cursor # LLM # Developer Tools
How to Build a Local AI API Gateway with CLIProxyAPI in 15 Minutes

Have you ever found yourself juggling multiple AI coding assistants? You use Cursor, switch to Claude Code for a different model, maybe dip into Copilot on the side. Each tool has its own authentication method, quota limits, and feels like playing hopscotch. Worse, many models use different protocols, forcing you to build custom adapters just to call them uniformly.

Last week, I spent half an afternoon wrestling with authentication when trying to integrate both GPT and Claude into an internal project. Then I found CLIProxyAPI. Within 5 minutes, I had a local proxy running, exposing all my AI models through a single OpenAI-compatible API. Cursor, Continue, or even custom Python scripts could hook right in.

This guide will walk you through setting it up from scratch: install the service, configure a provider, make your first request via curl, and integrate it into a real workflow. By the end, you'll have your own unified AI gateway.

Prerequisites

  • Go Environment: Go 1.21+ required (for compilation; local dev recommends standard GOPATH or Go modules).
  • Basic CLI Skills: Familiarity with git clone, cd, curl on Linux/macOS is enough.
  • At least one AI Account: Claude Code / ChatGPT / Kimi / Gemini works (uses OAuth).
  • Optional: Docker (for containerized deployment).

Note: No need to dive into Go source code. We'll treat it purely as a runnable service.

Step 1: Clone & Build

First, pull the repository and compile:

bash 复制代码
git clone https://github.com/router-for-me/CLIProxyAPI.git
cd CLIProxyAPI
go mod tidy && go build -o cliproxyapi .

After compilation, a cliproxyapi binary appears in the current directory. The process typically takes 1-2 minutes depending on your network.

Why compile instead of downloading? Go produces statically linked binaries with zero external dependencies, making server deployment a breeze. You could also use go install or grab a Release package, but compiling ensures perfect version alignment with the source.

Step 2: Start the Service & Configure a Provider

CLIProxyAPI listens on a local port (default is usually 8080 or 1323), forwarding requests uniformly to configured AI providers.

Minimal Start Command

bash 复制代码
./cliproxyapi --port 8080

You'll see logs similar to:

复制代码
[CLIProxyAPI] Server started on :8080
[CLIProxyAPI] OpenAI-compatible endpoint ready

The service is now running, but it needs a provider to actually route requests.

OAuth Integration for Claude Code (Recommended for Beginners)

Claude Code uses an OAuth flow, so you don't need to manually manage API keys. Open your browser and visit:

复制代码
http://localhost:8080/oauth/claude

Follow the prompts to log in to your Anthropic account. Once authorized, CLIProxyAPI automatically fetches the token. From then on, all /v1/chat/completions requests will route to Claude models.

Integrating Other Providers

Similarly, the project supports:

  • OpenAI Codex: http://localhost:8080/oauth/openai
  • Google Gemini (Antigravity): http://localhost:8080/oauth/antigravity
  • xAI Grok Build: http://localhost:8080/oauth/grok
  • Kimi: Retrieve an API Key from the Kimi Open Platform or add it directly to the configuration.

Why start with OAuth? It's the least friction path for developers—no key rotation, no config files, just a browser click. For quick personal validation, it's the optimal route.

Step 3: First API Call – Verify Connectivity

Open a new terminal and test the local proxy with curl:

bash 复制代码
curl http://localhost:8080/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
    "model": "claude-sonnet-4-20250514",
    "messages": [{"role": "user", "content": "Explain microservices in one sentence"}]
  }'

You'll get a standard OpenAI Chat Completion response:

json 复制代码
{
  "id": "chatcmpl-xxx",
  "object": "chat.completion",
  "created": 1726750000,
  "choices": [{
    "index": 0,
    "message": {
      "role": "assistant",
      "content": "Microservices architecture breaks down large applications into smaller, independently deployable services, each handling a specific concern and communicating over a network..."
    },
    "finish_reason": "stop"
  }],
  "usage": {"prompt_tokens": 20, "completion_tokens": 45, "total_tokens": 65}
}

Seeing content in choices[0].message.content means your proxy gateway is fully operational. It's now a standard OpenAI-compatible endpoint, and any tool supporting the OpenAI SDK can connect directly.

Practical Integration: Routing Cursor Through CLIProxyAPI

Let's plug it into Cursor, one of the most popular AI coding tools.

Steps

  1. Open Cursor Settings: Navigate to Settings → Features → AI.
  2. Add a Custom API Provider:
    • API Base URL: http://localhost:8080/v1
    • API Key: local-proxy (placeholder; OAuth handles actual auth)
    • Model: claude-sonnet-4-20250514 (or your preferred model)
  3. Save & Test: Return to the Chat panel, type a prompt like "Explain this code", and verify the response.

If your Cursor supports multi-model switching, you can configure both Claude and GPT providers simultaneously and toggle them in-editor. That's the power of a unified API: no tool modifications needed, just swap the endpoint.

Multi-Account Load Balancing (Advanced)

CLIProxyAPI supports configuring multiple accounts per provider with automatic round-robin distribution. This is highly valuable for teams: if one account hits its quota, it automatically fails over to the next.

Configuration varies by provider, typically handled via a config file or Management API. For Claude specifically, after completing multiple OAuth logins, CLIProxyAPI automatically manages the account pool and distributes requests using a round-robin strategy.

Note: For production-grade usage, consider pairing it with third-party management tools like CPA-Manager-Plus. It provides a visual dashboard for monitoring quotas, latency, token consumption, and auto-removing unhealthy accounts.

FAQ

Q: Service fails to start due to port conflict?
A: The default port might be occupied. Add --port 9090 (or any free port) to specify a new one, and update the caller's Base URL accordingly.

Q: curl returns 401 or an empty response?
A: OAuth authorization is likely incomplete. Revisit http://localhost:8080/oauth/claude (replace with your provider's path) and ensure successful login.

Q: How to deploy to a remote server?
A: After compiling the binary, run it in the background using nohup ./cliproxyapi --port 8080 > log.txt 2>&1 &, or package it with Docker. Remember to configure firewall rules to only allow trusted IPs to access port 8080.

Q: Does it support streaming responses?
A: Yes. Add "stream": true to your JSON payload. The service returns data in Server-Sent Events (SSE) format, token by token. Tools like Cursor and Continue enable streaming by default.

Summary

Today we:

  1. Compiled CLIProxyAPI from source
  2. Authenticated via OAuth for at least one AI provider
  3. Verified API connectivity with curl
  4. Integrated Cursor for seamless multi-model switching

The core concept is simple: flatten provider differences behind a single OpenAI-compatible interface. Point any AI tool to your local proxy, and let CLIProxyAPI handle routing, authentication, and load balancing.

If you want to dive deeper, try these next steps:

  • Read docs/sdk-usage.md in the repo to learn how to embed CLIProxyAPI directly in Go code
  • Explore the Management API for dynamic runtime configuration
  • Pair with CPA Usage Keeper for visual usage tracking

Drop your questions or experiences in the comments. Happy coding!

Last Updated:2026-09-19 10:04:42

Comments (0)

Post Comment

Loading...
0/500
Loading comments...