New APINew API
User GuideInstallationAPI ReferenceAI ApplicationsSkillsHelp & SupportBusiness Cooperation

Environment Variable Configuration Guide

This document provides all environment variables supported by New API and their configuration instructions. You can customize the system's behavior by setting these environment variables.

Tip

New API supports reading environment variables from .env files. Please refer to the .env.example file and rename it to .env when using it.

Basic Configuration

Environment VariableDescriptionDefault ValueExample
PORTService listening port3000PORT=8080
TZTime zone setting-TZ=America/New_York
VERSIONOverride running version number-VERSION=1.2.3

Database Configuration

Environment VariableDescriptionDefault ValueExample
SQL_DSNDatabase connection stringSQLite (one-api.db)PostgreSQL (recommended): SQL_DSN=postgresql://root:123456@postgres:5432/new-api | MySQL: SQL_DSN=root:123456@tcp(localhost:3306)/new-api
SQL_MAX_IDLE_CONNSMaximum idle connections in connection pool100SQL_MAX_IDLE_CONNS=50
SQL_MAX_OPEN_CONNSMaximum open connections in connection pool1000SQL_MAX_OPEN_CONNS=500
SQL_MAX_LIFETIMEMaximum connection lifetime (seconds)60SQL_MAX_LIFETIME=120
LOG_SQL_DSNSeparate database connection string for log table-LOG_SQL_DSN=postgresql://root:123456@postgres:5432/oneapi_logs
SQLITE_PATHSQLite database pathone-api.dbSQLITE_PATH=/var/lib/new-api/new-api.db

Database recommendation

PostgreSQL is preferred for production. MySQL remains a compatible option; SQLite, used when SQL_DSN is unset, is better suited to local development and testing.

Cache Configuration

Environment VariableDescriptionDefault ValueExample
REDIS_CONN_STRINGOne Redis-compatible connection endpoint; include credentials in the URI-REDIS_CONN_STRING=redis://default:redispw@localhost:6379
REDIS_POOL_SIZERedis connection pool size10REDIS_POOL_SIZE=20
MEMORY_CACHE_ENABLEDWhether to enable memory cache (automatically enabled when Redis is enabled)falseMEMORY_CACHE_ENABLED=true
SYNC_FREQUENCYCache synchronization frequency and maximum Session-cache stale window with independent Redis (seconds)60SYNC_FREQUENCY=120
BATCH_UPDATE_ENABLEDEnable database batch update aggregationfalseBATCH_UPDATE_ENABLED=true
BATCH_UPDATE_INTERVALBatch update aggregation interval (seconds)5BATCH_UPDATE_INTERVAL=10

REDIS_CONN_STRING must point to one Redis-compatible endpoint. New API does not natively parse Redis Cluster or Sentinel node lists. For Redis high availability, use a managed service or proxy that exposes one compatible endpoint.

The database is authoritative for dashboard Sessions. Redis caches each Session for the smaller of its remaining lifetime and the effective SYNC_FREQUENCY, and reads do not renew that TTL. With independent Redis per node, Session revocation and version changes therefore converge after a database fallback within this window. Delayed active-cache fills keep only the unused portion of their original database-observation window.

Multi-node and Security Configuration

Environment VariableDescriptionDefault ValueExample
SESSION_SECRETSession secret; must be identical on every application node-SESSION_SECRET=your_session_secret
CRYPTO_SECRETHMAC secret for cache keys; nodes sharing Redis must use the same effective valueDefaults to SESSION_SECRETCRYPTO_SECRET=your_crypto_secret
FRONTEND_BASE_URLFrontend base URL (used by slave nodes, redirects unmatched routes to this address)-FRONTEND_BASE_URL=https://<your-newapi-domain>
NODE_TYPENode typemasterNODE_TYPE=slave
TLS_INSECURE_SKIP_VERIFYGlobally skip TLS certificate verificationfalseTLS_INSECURE_SKIP_VERIFY=true
TRUSTED_REDIRECT_DOMAINSList of trusted redirect domains (comma-separated)-TRUSTED_REDIRECT_DOMAINS=example.com,api.example.com

SESSION_SECRET Notes

SESSION_SECRET cannot be set to random_string, otherwise the program will refuse to start. All nodes must use the same secret for multi-machine deployment.

Redis Topology

Redis may be shared by all nodes, deployed independently per node, or omitted. Shared Redis normally propagates Session revocation immediately and shares Redis-backed rate-limit quotas. Independent Redis converges within the effective SYNC_FREQUENCY, but each node has a separate rate-limit quota. Without Redis, Session validation reads the database and rate limiting uses node-local memory.

Cluster Deployment

For how to build a complete cluster deployment using these environment variables, please refer to the Cluster Deployment Guide.

User and Token Configuration

Environment VariableDescriptionDefault ValueExample
GENERATE_DEFAULT_TOKENGenerate initial token for new registered usersfalseGENERATE_DEFAULT_TOKEN=true
NOTIFICATION_LIMIT_DURATION_MINUTENotification limit duration (minutes)10NOTIFICATION_LIMIT_DURATION_MINUTE=15
NOTIFY_LIMIT_COUNTMaximum number of notifications within the specified duration2NOTIFY_LIMIT_COUNT=3

Request Limit Configuration

Environment VariableDescriptionDefault ValueExample
GLOBAL_API_RATE_LIMIT_ENABLEGlobal API rate limit switchtrueGLOBAL_API_RATE_LIMIT_ENABLE=false
GLOBAL_API_RATE_LIMITGlobal API rate limit (per IP)180GLOBAL_API_RATE_LIMIT=100
GLOBAL_API_RATE_LIMIT_DURATIONGlobal API rate limit window (seconds)180GLOBAL_API_RATE_LIMIT_DURATION=120
GLOBAL_WEB_RATE_LIMIT_ENABLEGlobal Web rate limit switchtrueGLOBAL_WEB_RATE_LIMIT_ENABLE=false
GLOBAL_WEB_RATE_LIMITGlobal Web rate limit (per IP)60GLOBAL_WEB_RATE_LIMIT=30
GLOBAL_WEB_RATE_LIMIT_DURATIONGlobal Web rate limit window (seconds)180GLOBAL_WEB_RATE_LIMIT_DURATION=120
CRITICAL_RATE_LIMIT_ENABLECritical operation rate limit switchtrueCRITICAL_RATE_LIMIT_ENABLE=false
CRITICAL_RATE_LIMITCritical operation rate limit count20CRITICAL_RATE_LIMIT=10
CRITICAL_RATE_LIMIT_DURATIONCritical operation rate limit window (seconds)1200CRITICAL_RATE_LIMIT_DURATION=600

Relay and Proxy Configuration

Environment VariableDescriptionDefault ValueExample
RELAY_TIMEOUTRelay request timeout (seconds), 0 for unlimited0RELAY_TIMEOUT=60
STREAMING_TIMEOUTStreaming single response timeout (seconds)300STREAMING_TIMEOUT=120
RELAY_MAX_IDLE_CONNSMaximum idle connections in relay HTTP client pool500RELAY_MAX_IDLE_CONNS=1000
RELAY_MAX_IDLE_CONNS_PER_HOSTMaximum idle connections per host in relay HTTP client pool100RELAY_MAX_IDLE_CONNS_PER_HOST=200
MAX_FILE_DOWNLOAD_MBMaximum file download size (MB)64MAX_FILE_DOWNLOAD_MB=128
MAX_REQUEST_BODY_MBMaximum request body size (MB, counted after decompression; prevents zip bomb/OOM)128MAX_REQUEST_BODY_MB=256
STREAM_SCANNER_MAX_BUFFER_MBSSE stream scanner maximum buffer size (MB)64STREAM_SCANNER_MAX_BUFFER_MB=128

RELAY_TIMEOUT Setting Warning

Please be cautious when setting the RELAY_TIMEOUT environment variable. Setting it too short may lead to the following issues:

  • Upstream API has completed the request and charged, but local billing failed due to timeout

  • Leading to billing desynchronization, which may result in system losses

  • It is recommended not to set it unless you know what you are doing

Channel Management Configuration

Environment VariableDescriptionDefault ValueExample
CHANNEL_UPDATE_FREQUENCYRegularly update channel balance (minutes)- (disabled if not set)CHANNEL_UPDATE_FREQUENCY=1440
CHANNEL_TEST_FREQUENCYRegularly check channels (minutes), overrides database settings- (uses database settings if not set)CHANNEL_TEST_FREQUENCY=1440
POLLING_INTERVALRequest polling interval (seconds)0POLLING_INTERVAL=5

Channel Upstream Model Synchronization

Environment VariableDescriptionDefault ValueExample
CHANNEL_UPSTREAM_MODEL_UPDATE_TASK_ENABLEDEnable regular synchronization of upstream provider model listtrueCHANNEL_UPSTREAM_MODEL_UPDATE_TASK_ENABLED=false
CHANNEL_UPSTREAM_MODEL_UPDATE_TASK_INTERVAL_MINUTESModel list synchronization interval (minutes)30CHANNEL_UPSTREAM_MODEL_UPDATE_TASK_INTERVAL_MINUTES=60
CHANNEL_UPSTREAM_MODEL_UPDATE_MIN_CHECK_INTERVAL_SECONDSMinimum check interval per channel (seconds)300CHANNEL_UPSTREAM_MODEL_UPDATE_MIN_CHECK_INTERVAL_SECONDS=600

Model and Request Processing Configuration

Environment VariableDescriptionDefault ValueExample
FORCE_STREAM_OPTIONOverride client stream_options parameter, force retrieval of streaming usage informationtrueFORCE_STREAM_OPTION=false
CountTokenWhether to enable local token countingtrueCountToken=false
GET_MEDIA_TOKENWhether to count image/audio tokens in streaming modetrueGET_MEDIA_TOKEN=false
GET_MEDIA_TOKEN_NOT_STREAMWhether to count image/audio tokens in non-streaming modefalseGET_MEDIA_TOKEN_NOT_STREAM=true

Asynchronous Task Configuration

Environment VariableDescriptionDefault ValueExample
UPDATE_TASKWhether to enable asynchronous task polling updates (MJ, Suno, etc.)trueUPDATE_TASK=false
TASK_QUERY_LIMITMaximum number of tasks queried per poll1000TASK_QUERY_LIMIT=500
TASK_TIMEOUT_MINUTESAsynchronous task timeout (minutes), marked as failed and refunded after timeout, 0 to disable1440TASK_TIMEOUT_MINUTES=720
TASK_PRICE_PATCHTask price patch, specified models are billed per-use only, model names separated by English commas-TASK_PRICE_PATCH=sora-2,sora-2-pro

Specific Model Configuration

Environment VariableDescriptionDefault ValueExample
AZURE_DEFAULT_API_VERSIONAzure channel default API version2025-04-01-previewAZURE_DEFAULT_API_VERSION=2023-05-15
COHERE_SAFETY_SETTINGCohere model safety settingNONECOHERE_SAFETY_SETTING=CONTEXTUAL
DIFY_DEBUGDify channel output workflow and node informationtrueDIFY_DEBUG=false

Subscription Cache Configuration

Environment VariableDescriptionDefault ValueExample
SUBSCRIPTION_PLAN_CACHE_TTLSubscription plan cache TTL (seconds)300SUBSCRIPTION_PLAN_CACHE_TTL=600
SUBSCRIPTION_PLAN_INFO_CACHE_TTLSubscription plan details cache TTL (seconds)120SUBSCRIPTION_PLAN_INFO_CACHE_TTL=300
SUBSCRIPTION_PLAN_CACHE_CAPSubscription plan cache maximum capacity5000SUBSCRIPTION_PLAN_CACHE_CAP=10000
SUBSCRIPTION_PLAN_INFO_CACHE_CAPSubscription plan details cache maximum capacity10000SUBSCRIPTION_PLAN_INFO_CACHE_CAP=20000

Other Configuration

Environment VariableDescriptionDefault ValueExample
ERROR_LOG_ENABLEDWhether to log and display error logs on the frontendfalseERROR_LOG_ENABLED=true

Analytics and Statistics

Environment VariableDescriptionDefault ValueExample
UMAMI_WEBSITE_IDUmami Website ID-UMAMI_WEBSITE_ID=xxxx-xxxx
UMAMI_SCRIPT_URLUmami Script URLhttps://analytics.umami.is/script.jsUMAMI_SCRIPT_URL=https://umami.example.com/script.js
GOOGLE_ANALYTICS_IDGoogle Analytics 4 Website ID-GOOGLE_ANALYTICS_ID=G-XXXXXXX

Metadata Synchronization

Environment VariableDescriptionDefault ValueExample
SYNC_UPSTREAM_BASEModel/Vendor Metadata Upstream Addresshttps://basellm.github.io/llm-metadataSYNC_UPSTREAM_BASE=https://mirror.example.com/llm-metadata
SYNC_HTTP_TIMEOUT_SECONDSSync HTTP Timeout (seconds)10SYNC_HTTP_TIMEOUT_SECONDS=15
SYNC_HTTP_RETRYSync Retry Count3SYNC_HTTP_RETRY=5
SYNC_HTTP_MAX_MBMax Response Body Size (MB)10SYNC_HTTP_MAX_MB=20

Frontend Configuration

Environment VariableDescriptionDefault ValueExample
VITE_REACT_APP_SERVER_URLFrontend base URL for backend requests (effective at compile time)-VITE_REACT_APP_SERVER_URL=https://api.example.com

Compile-time Variables

VITE_REACT_APP_SERVER_URL is a frontend variable injected by Vite at compile time, only effective during frontend build, cannot be modified at runtime.

Pyroscope Continuous Performance Analysis

Environment VariableDescriptionDefault ValueExample
PYROSCOPE_URLPyroscope server address, leave blank to disable-PYROSCOPE_URL=http://pyroscope:4040
PYROSCOPE_APP_NAMEApplication name for reportingnew-apiPYROSCOPE_APP_NAME=my-api
PYROSCOPE_BASIC_AUTH_USERPyroscope Basic Auth username-PYROSCOPE_BASIC_AUTH_USER=admin
PYROSCOPE_BASIC_AUTH_PASSWORDPyroscope Basic Auth password-PYROSCOPE_BASIC_AUTH_PASSWORD=secret
HOSTNAMEHostname label for reportingnew-apiHOSTNAME=node-1
PYROSCOPE_MUTEX_RATEMutex profiling sample rate5PYROSCOPE_MUTEX_RATE=10
PYROSCOPE_BLOCK_RATEBlock profiling sample rate5PYROSCOPE_BLOCK_RATE=10

LinuxDo OAuth

No modification is required under normal circumstances.

Environment VariableDescriptionDefault ValueExample
LINUX_DO_TOKEN_ENDPOINTLinuxDo Token Endpointhttps://connect.linux.do/oauth2/tokenLINUX_DO_TOKEN_ENDPOINT=https://connect.linux.do/oauth2/token
LINUX_DO_USER_ENDPOINTLinuxDo User Endpointhttps://connect.linux.do/api/userLINUX_DO_USER_ENDPOINT=https://connect.linux.do/api/user
Environment VariableDescriptionDefault ValueExample
DEBUGEnable debug mode (including GORM debug output)falseDEBUG=true
GIN_MODEGin running modereleaseGIN_MODE=debug
ENABLE_PPROFEnable pprof performance analysis (port 8005)falseENABLE_PPROF=true

Deprecated Environment Variables

The following environment variables have been deprecated. Please use the corresponding options in the system settings interface:

Environment VariableAlternative Method
GEMINI_MODEL_MAPPlease set in System Settings - Model Related Settings
GEMINI_SAFETY_SETTINGPlease set in System Settings - Model Related Settings
GEMINI_VISION_MAX_IMAGE_NUMPlease set in System Settings - Model Related Settings

Multi-machine Deployment Example

Multi-machine deployments must use the same database and the same SESSION_SECRET. Redis may be shared by all nodes, deployed independently per node, or omitted; see the Cluster Deployment Guide for the behavioral differences.

Master Node Configuration

# Database Configuration - Use a remote database
SQL_DSN=postgresql://newapi:password@db-server:5432/oneapi

# Security Configuration
SESSION_SECRET=your_unique_session_secret
CRYPTO_SECRET=your_unique_crypto_secret

# Redis Cache Configuration (may instead use a node-local Redis, or be omitted)
REDIS_CONN_STRING=redis://default:password@redis-server:6379

Slave Node Configuration

# Database Configuration - Use the same remote database
SQL_DSN=postgresql://newapi:password@db-server:5432/oneapi

# Security Configuration - SESSION_SECRET must match the master
SESSION_SECRET=your_unique_session_secret
CRYPTO_SECRET=your_unique_crypto_secret # Must match when Redis is shared

# Redis Cache Configuration - May match the master or point to this node's independent Redis
REDIS_CONN_STRING=redis://default:password@redis-slave-node:6379

# Node Type Setting
NODE_TYPE=slave

# Optional: Frontend Base URL
# FRONTEND_BASE_URL=https://<your-newapi-domain>

# Optional: Synchronization Frequency
SYNC_FREQUENCY=60

Complete Cluster Configuration

This example shows independent Redis instances. To share Redis, use the same REDIS_CONN_STRING on both nodes. Without Redis, Session validation reads the shared database directly. For complete consistency semantics and deployment guidance, see the Cluster Deployment Guide.

Environment Variable Example in Docker Compose

Below is a brief example of setting environment variables in a Docker Compose configuration file:

services:
  new-api:
    image: calciumion/new-api:latest
    environment:
      - TZ=Asia/Shanghai
      - SQL_DSN=postgresql://newapi:123456@postgres:5432/oneapi
      - REDIS_CONN_STRING=redis://default:redispw@redis:6379
      - SESSION_SECRET=your_unique_session_secret
      - CRYPTO_SECRET=your_unique_crypto_secret
      - MEMORY_CACHE_ENABLED=true
      - GENERATE_DEFAULT_TOKEN=true
      - STREAMING_TIMEOUT=120
      - CHANNEL_UPDATE_FREQUENCY=1440

For a complete Docker Compose configuration, including more environment variable setting options, please refer to the Docker Compose Configuration Instructions document.

How is this guide?

Last updated on