A robust backend service that provides a unified API interface for interacting with multiple Large Language Model (LLM) providers, including OpenAI, Google Gemini, Anthropic, and DeepSeek. This project simplifies the integration of various AI models into your applications by offering a consistent chat interface, built-in API key management, automatic usage tracking, and pay-per-token billing.
- Multi-Provider Support: Seamlessly switch between OpenAI, Google Gemini, Anthropic, and DeepSeek models through a single API.
- Unified Chat API: A single
POSTendpoint to interact with all supported LLMs β just specify the provider and model. - API Key Management: Generate, list, and delete API keys securely, scoped per user.
- Automatic Usage Tracking: Every chat request automatically logs token consumption (input, output, total) per API key.
- Pay-Per-Token Billing: Price calculations based on a configurable pricing table (
pricingtable in Supabase) with a 5% commission layer. - Usage & Billing Endpoint: Check aggregated usage stats and the final price for a user across all their API keys.
- Authentication: Two-layer auth middleware β user-only validation for key management, and user + API key validation for chat requests.
- Model Pricing Reference: Includes a comprehensive CSV (
Token-Prices/Model_Pricings.csv) with per-token pricing for 79+ models across all providers. - Scalable Architecture: Built with Node.js and Express 5, designed for easy provider extension via the factory pattern.
| Category | Technology |
|---|---|
| Runtime | Node.js (v16+) |
| Framework | Express.js v5 |
| Database / Auth | Supabase (PostgreSQL + Auth) |
| AI SDKs | openai (OpenAI, Gemini & DeepSeek via compatibility layer), @anthropic-ai/sdk |
| Utilities | dotenv (env config), nodemon (dev hot-reload) |
Before you begin, ensure you have the following:
- Node.js (v16 or higher) & npm
- A Supabase project with the following tables configured:
Usersβ user records withUser_namecolumnAPI_Keysβ stores generated keys withAPI_keyandUser_namecolumnspricingβ model pricing withprovider,model, andprice_per_tokencolumnsapi_usageβ usage logs withapi_key,provider,model,input_tokens,output_tokens,total_tokens, andpricecolumns
- API Keys for the providers you intend to use:
-
Clone the repository
git clone <repository_url> cd Backend
-
Install dependencies
npm install
-
Configure Environment Variables Create a
.envfile in the root directory:PORT=3000 # AI Provider Keys OPENAI_API_KEY=your_openai_api_key_here GEMINI_API_KEY=your_gemini_api_key_here ANTHROPIC_API_KEY=your_anthropic_api_key_here DEEPSEEK_API_KEY=your_deepseek_api_key_here # Supabase Configuration SUPABASE_URL=your_supabase_project_url SUPABASE_KEY=your_supabase_anon_key
Use nodemon to automatically restart the server on file changes:
npm run devStart the server normally:
npm startThe server will start on http://localhost:3000 (or the port specified in your .env).
All endpoints require authentication headers. See the Authentication section below for details.
Send a message to any supported LLM provider.
Headers:
| Header | Required | Description |
|---|---|---|
user |
β | Your registered username |
api-key |
β | A valid API key generated for your account |
Request Body:
{
"providerName": "openai",
"model_name": "gpt-4o",
"message": "Hello, how are you?"
}Supported Providers: openai, gemini, anthropic, deepseek
Response:
{
"content": "I'm doing great! How can I help you today?",
"usage": {
"inputTokens": 12,
"outputTokens": 15,
"totalTokens": 27
}
}Note: Usage is automatically logged to the
api_usagetable with price calculation after every successful request.
Manage API keys for accessing the service. These endpoints require a valid user header.
Retrieve all API keys associated with the authenticated user.
Generate a new API key for the authenticated user.
Revoke an existing API key.
Request Body:
{
"key": "your_api_key_to_delete"
}Get aggregated usage statistics and the calculated bill for the authenticated user.
Headers:
| Header | Required | Description |
|---|---|---|
user |
β | Your registered username |
api-key |
β | A valid API key |
Response:
{
"priceToPay": {
"username": "john_doe",
"total_input_tokens": 15240,
"total_output_tokens": 8320,
"total_tokens": 23560,
"base_price": 0.029450,
"commission_5_percent": 0.001473,
"final_price": 0.030923
}
}The API uses two levels of middleware:
| Middleware | Used By | Validates |
|---|---|---|
auth.middleware.js |
Chat, Usage | user header (against Users table) + api-key header (against API_Keys table) |
authForKeys.middleware.js |
Key management | user header only (against Users table) |
The billing system works in three steps:
- Per-Request Price Calculation (
priceCal.logistics.js): After each chat request, the system looks up the per-token price from thepricingtable for the given provider/model and logs the usage with the calculated cost. - Aggregated Billing (
finalPrice.logistics.js): When a user checks their usage, all usage records across all their API keys are summed up, and a 5% commission is applied on top of the base price. - Model Pricing Reference: The
Token-Prices/Model_Pricings.csvfile contains per-token pricing for 79 models across all four providers, which can be used to seed thepricingtable.
βββ Token-Prices/
β βββ Model_Pricings.csv # Reference pricing data for 79+ models
βββ src/
β βββ controller/
β β βββ chat.controller.js # Chat request handler
β β βββ keys.controller.js # API key CRUD handler
β β βββ usage.controller.js # Usage & billing handler
β βββ logistics/
β β βββ priceCal.logistics.js # Per-request price calculation & usage logging
β β βββ finalPrice.logistics.js # Aggregated billing with commission
β βββ middleware/
β β βββ auth.middleware.js # User + API Key validation
β β βββ authForKeys.middleware.js # User-only validation
β β βββ error.middleware.js # Global error handler
β βββ model/
β β βββ dbClient.js # Database client setup
β β βββ supabaseClient.js # Supabase client initialization
β βββ provider/
β β βββ openai.provider.js # OpenAI integration
β β βββ gemini.provider.js # Google Gemini integration (via OpenAI compat)
β β βββ anthropic.provider.js # Anthropic Claude integration
β β βββ deepseek.provider.js # DeepSeek integration (via OpenAI compat)
β β βββ providerManager.provider.js # Provider factory / registry
β βββ routes/
β β βββ chat.routes.js # /api/layers/* routes
β β βββ keys.routes.js # /api/keys/* routes
β β βββ usage.routes.js # /api/usage/* routes
β βββ services/
β β βββ chat.services.js # Chat business logic + usage logging
β β βββ generateKey.services.js # API key generation logic
β βββ app.js # Express app setup & route mounting
β βββ index.js # Server entry point
βββ .env # Environment variables (not committed)
βββ .gitignore
βββ package.json
βββ README.md
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the project
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project is licensed under the ISC License.