Fly.io (flyctl + Machines API)
Fly.io is a compute platform for deploying containerized apps globally. There is no official Fly.io Python SDK — interaction is via the flyctl CLI, fly.toml config file, and the Machines REST API. The unofficial fly-python-sdk on PyPI (pip install fly-python-sdk) is a third-party project last updated October 2023 and should not be relied upon.
Warnings
- breaking Apps V1 (Nomad-based) was fully removed March 2023. All apps migrated to V2 (Machines). Old fly.toml files using legacy [[services]] format may need updating to [http_service] for simple HTTP apps.
- breaking auto_stop_machines previously accepted boolean true/false. Now requires string values: 'off', 'stop', or 'suspend'. Boolean true still maps to 'stop' but causes a deprecation warning and will eventually break.
- breaking Free tier removed for new organizations created after October 7, 2024. New accounts are pay-as-you-go only. Existing users keep legacy free allowances unless they switch plans.
- gotcha fly launch creates two Machines by default on first deploy if [http_service] is configured (redundancy by default). This doubles cost unexpectedly for hobby projects.
- gotcha Apps must listen on 0.0.0.0:<internal_port>, not 127.0.0.1 or localhost. Listening on localhost causes 'app is not listening on the expected address' deploy warning and the app is unreachable.
- gotcha Machine suspend (auto_stop_machines = 'suspend') requires ≤2GB RAM, no swap configured, no GPU, and no schedule. Machines not meeting requirements silently fall back to 'stop' behavior.
- gotcha fly.toml changes do not take effect until fly deploy is run. Saving fly.toml does not trigger a redeploy. Dashboard view of fly.toml may lag behind actual deployed config.
- gotcha flyctl auto-updates by default. New flyctl versions occasionally change behavior or add new fly.toml schema fields that conflict with old configs. Pinning flyctl version in CI is recommended.
- breaking Python packages in `requirements.txt` or `pyproject.toml` may specify a minimum Python version. If the deployment environment (e.g., `fly.toml` buildpack settings, Dockerfile base image) uses an older Python version, package installation will fail with 'Requires-Python' errors.
- gotcha Test output shows pip warnings regarding running as the 'root' user and an available update. Running pip as root can cause permission issues, and an outdated pip might lead to unexpected behavior or missed features.
Install
-
curl -L https://fly.io/install.sh | sh -
brew install flyctl -
pip install fly-python-sdk
Imports
- Machines API (Python)
import requests headers = {"Authorization": f"Bearer {FLY_API_TOKEN}"} requests.get("https://api.machines.dev/v1/apps/{app_id}/machines", headers=headers)
Quickstart
# Deploy a Python app: # 1. In your project directory: fly launch # Scans project, creates fly.toml, provisions app fly deploy # Builds image, deploys to Machines # Minimal fly.toml for a Python web app: # app = 'my-app' # primary_region = 'iad' # # [http_service] # internal_port = 8080 # force_https = true # auto_stop_machines = 'suspend' # auto_start_machines = true # min_machines_running = 0 # # [[vm]] # memory = '256mb' # cpu_kind = 'shared' # cpus = 1 # Secrets (env vars): fly secrets set OPENAI_API_KEY=sk-... # Scale: fly scale count 2 fly scale memory 512