A tiny reverse proxy in Go that forwards requests to an OpenAI-compatible API. Useful for:
- Centralizing API key management
- Swapping between OpenAI-compatible providers by changing a single URL
- Running locally or in a container with minimal configuration
The proxy:
- Forwards method, path, query, and most headers unmodified
- Optionally injects an Authorization header from a server-side API key
- Streams responses (Server-Sent Events / chunked responses) through to clients
main.go— Proxy server implementationgo.mod— Go module definition (Go 1.22)Dockerfile— Multi-stage build for a small, static container image.gitignore— Common Go and IDE ignores
- Go 1.22+ (for local builds), or
- Docker (for containerized runs)
You can configure the proxy using flags or environment variables. Environment variables are used when the corresponding flag is not provided.
Flags:
-openai-key— API key to inject asAuthorization: Bearer <key>in outgoing requests-openai-url— Target base URL for the OpenAI-compatible API (default:https://api.openai.com)-port— Port to run the proxy on (default:8080)
Environment variables:
OPENAI_API_KEY— Same as-openai-keyOPENAI_URL— Same as-openai-urlPORT— Same as-port
Behavior:
- If an API key is provided (via
-openai-keyorOPENAI_API_KEY), the proxy setsAuthorization: Bearer <key>on all outgoing requests, overriding any client-provided Authorization header. - If no API key is provided, the proxy forwards the client's Authorization header as-is.
- All other headers are forwarded unmodified.
- Clone and enter the repository, then:
-
With environment variables:
export OPENAI_API_KEY=sk-xxxx export OPENAI_URL=https://api.openai.com export PORT=8080 go run . -
With flags:
go run . -openai-key=sk-xxxx -openai-url=https://api.openai.com -port=8080
The server will start on http://localhost:8080 and forward to the configured OPENAI_URL.
Pull and run the pre-built image from Docker Hub:
docker pull alexsergin/openai-proxy:latest
docker run -d -p 8080:8080 \
-e OPENAI_API_KEY=sk-xxxx \
-e OPENAI_URL=https://api.openai.com \
alexsergin/openai-proxy:latestOr run directly without pulling first:
docker run -d -p 8080:8080 \
-e OPENAI_API_KEY=sk-xxxx \
-e OPENAI_URL=https://api.openai.com \
alexsergin/openai-proxy:latestThe proxy will be available at http://localhost:8080.