Skip to content

alexsergin/openai-proxy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OpenAI Proxy (Go 1.22)

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

Project Structure

  • main.go — Proxy server implementation
  • go.mod — Go module definition (Go 1.22)
  • Dockerfile — Multi-stage build for a small, static container image
  • .gitignore — Common Go and IDE ignores

Requirements

  • Go 1.22+ (for local builds), or
  • Docker (for containerized runs)

Configuration

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 as Authorization: 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-key
  • OPENAI_URL — Same as -openai-url
  • PORT — Same as -port

Behavior:

  • If an API key is provided (via -openai-key or OPENAI_API_KEY), the proxy sets Authorization: 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.

Quick Start

Run locally (Go)

  1. 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.

Run with Docker

Using the pre-built image (recommended)

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:latest

Or 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:latest

The proxy will be available at http://localhost:8080.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors