APIFold

Self-Hosting APIFold

APIFold is designed to be self-hosted on any server that supports Docker and Docker Compose. This guide will walk you through setting up the platform on your own infrastructure.

Prerequisites

Before you begin, ensure your server meets the following requirements:

  • Docker: v24.0 or newer
  • Docker Compose: v2.0 or newer
  • Resources: Minimum 2 vCPU, 4GB RAM recommended
  • Domain Name: Optional, but recommended for production (required for SSL)

Quick Start

Get APIFold running in minutes with these commands:

# 1. Clone the repository
git clone https://github.com/Work90210/APIFold.git
cd APIFold
 
# 2. Configure environment variables
cp .env.example .env
# Edit .env with your secrets (see Configuration Reference below)
nano .env
 
# 3. Start the stack
docker compose -f infra/docker-compose.yml up -d

Your instance will be available at http://localhost:3000 (or your domain).

Configuration Reference

APIFold is configured via environment variables.

VariableDescriptionDefaultRequired
DATABASE_URLPostgreSQL connection string-Yes
DATABASE_POOL_MAXMax database connections20No
POSTGRES_DBDatabase nameapifoldNo
POSTGRES_USERDatabase usermtNo
POSTGRES_PASSWORDDatabase password-Yes
REDIS_PASSWORDRedis password-Yes
REDIS_URLRedis connection string-Yes
VAULT_SECRETEncryption key (32+ chars)-Yes
VAULT_SALTEncryption salt (32+ chars)-Yes
MCP_RUNTIME_SECRETInternal service auth secret-Yes
NODE_ENVEnvironment modedevelopmentNo
PORTWeb app port3000No
RUNTIME_PORTRuntime server port3001No
MAX_SSE_SESSIONSMax concurrent SSE connections10000No
SSE_HEARTBEAT_INTERVAL_MSKeep-alive ping interval30000No
SSE_IDLE_TIMEOUT_MSDisconnect idle clients after300000No
CIRCUIT_BREAKER_THRESHOLDFailures before opening circuit5No
CIRCUIT_BREAKER_COOLDOWN_MSTime before retrying upstream30000No
UPSTREAM_TIMEOUT_MSMax time for upstream response30000No
CREDENTIAL_TTL_MSCache duration for credentials300000No
FALLBACK_POLL_INTERVAL_MSDatabase polling frequency30000No
CORS_ORIGINSAllowed CORS origins (comma-separated)*Yes (Prod)
GLOBAL_RATE_LIMIT_WINDOW_MSRate limit window900000No
GLOBAL_RATE_LIMIT_MAXMax requests per window1000No
DRAIN_TIMEOUT_MSGraceful shutdown timeout30000No
LOG_LEVELLogging verbosityinfoNo

TLS Setup

For production deployments, secure your instance with SSL/TLS using Certbot.

  1. Install Certbot on your host machine
  2. Generate certificates for your domain
  3. Mount the certificates into the nginx container:
# infra/docker-compose.yml
services:
  nginx:
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
      - /etc/letsencrypt:/etc/letsencrypt:ro
  1. Update infra/nginx/nginx.conf with your domain name in the ssl_certificate and ssl_certificate_key paths.

Auto-Deploy

Keep your instance up-to-date automatically using Watchtower. Add this service to your docker-compose.yml:

services:
  watchtower:
    image: containrrr/watchtower
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    command: --interval 300

Security note: Mounting /var/run/docker.sock grants the Watchtower container root-equivalent access to the host. Only use this in trusted environments. Consider restricting Watchtower to specific containers using --scope labels, or use a CI/CD pipeline for production deployments instead.

Monitoring

APIFold exposes several endpoints for monitoring health and performance:

  • Health Check: GET /api/health (web) / GET /health (runtime) — Returns 200 OK
  • Liveness Probe: GET /health/live
  • Readiness Probe: GET /health/ready

Nginx access logs are output in JSON format for easy ingestion by logging systems like ELK, Datadog, or CloudWatch.

Backup & Restore

Regular backups are crucial. Use standard PostgreSQL tools to back up your data:

# Backup
docker exec -t apifold-postgres pg_dump -U mt apifold > backup.sql
 
# Restore
cat backup.sql | docker exec -i apifold-postgres psql -U mt apifold

Ensure you also back up your .env file containing encryption keys. Without the VAULT_SECRET and VAULT_SALT, your encrypted credentials cannot be recovered.

Upgrading

To upgrade to the latest version of APIFold:

  1. Pull the latest images:

    docker compose -f infra/docker-compose.yml pull
  2. Restart the containers:

    docker compose -f infra/docker-compose.yml up -d

Database migrations run automatically on startup.

Troubleshooting

Common issues and solutions:

IssuePossible CauseSolution
Container won't startDatabase not readyCheck docker logs for connection errors. Ensure Postgres is healthy.
Redis connection refusedWrong host/passwordVerify REDIS_URL matches the service name in Docker Compose (e.g., redis://:password@redis:6379).
SSE disconnectsNginx timeoutIncrease proxy_read_timeout in nginx config to match SSE_IDLE_TIMEOUT_MS.
502 Bad GatewayRuntime/Web downCheck container status with docker ps. View logs for crashes.
Encryption errorsMissing vault keysEnsure VAULT_SECRET and VAULT_SALT are set and at least 32 characters.

On this page