dify/api
zyssyz123 7c12e923b6
Some checks are pending
autofix.ci / autofix (push) Waiting to run
Build and Push API & Web / build (api, DIFY_API_IMAGE_NAME, linux/amd64, build-api-amd64) (push) Waiting to run
Build and Push API & Web / build (api, DIFY_API_IMAGE_NAME, linux/arm64, build-api-arm64) (push) Waiting to run
Build and Push API & Web / build (web, DIFY_WEB_IMAGE_NAME, linux/amd64, build-web-amd64) (push) Waiting to run
Build and Push API & Web / build (web, DIFY_WEB_IMAGE_NAME, linux/arm64, build-web-arm64) (push) Waiting to run
Build and Push API & Web / create-manifest (api, DIFY_API_IMAGE_NAME, merge-api-images) (push) Blocked by required conditions
Build and Push API & Web / create-manifest (web, DIFY_WEB_IMAGE_NAME, merge-web-images) (push) Blocked by required conditions
Main CI Pipeline / Check Changed Files (push) Waiting to run
Main CI Pipeline / API Tests (push) Blocked by required conditions
Main CI Pipeline / Web Tests (push) Blocked by required conditions
Main CI Pipeline / Style Check (push) Waiting to run
Main CI Pipeline / VDB Tests (push) Blocked by required conditions
Main CI Pipeline / DB Migration Test (push) Blocked by required conditions
feat: add trial model list in system features (#31313)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: hj24 <mambahj24@gmail.com>
2026-01-26 11:52:05 +08:00
..
.idea
.vscode feat: introduce trigger functionality (#27644) 2025-11-12 17:59:37 +08:00
configs feat: app trial (#26281) 2026-01-22 15:42:54 +08:00
constants feat(i18n): add Tunisian Arabic (ar-TN) translation (#29306) 2025-12-13 10:55:04 +08:00
context fix: use thread local isolation the context (#31410) 2026-01-22 18:02:54 +08:00
contexts feat: introduce trigger functionality (#27644) 2025-11-12 17:59:37 +08:00
controllers refactor: example of refine dict / Mapping (#31498) 2026-01-26 10:23:38 +08:00
core refactor: example of refine dict / Mapping (#31498) 2026-01-26 10:23:38 +08:00
docker feat: use more universal C.UTF-8 instead of en_US.UTF-8 (#30621) 2026-01-06 16:39:04 +08:00
enums feat: add trial model list in system features (#31313) 2026-01-26 11:52:05 +08:00
events feat: credential sync fix for enterprise edition (#30626) 2026-01-14 23:33:24 -08:00
extensions feat: init fastopenapi (#30453) 2026-01-23 21:07:52 +09:00
factories fix: validation error when uploading images with None URL values (#31012) 2026-01-15 10:54:10 +08:00
fields feat: archive workflow run logs backend (#31310) 2026-01-23 13:11:56 +08:00
libs feat: archive workflow run logs backend (#31310) 2026-01-23 13:11:56 +08:00
migrations feat: archive workflow run logs backend (#31310) 2026-01-23 13:11:56 +08:00
models refactor: single-node workflow runner helpers (#31472) 2026-01-24 10:27:44 +08:00
repositories feat: archive workflow run logs backend (#31310) 2026-01-23 13:11:56 +08:00
schedule feat: add lock for retention jobs (#31320) 2026-01-21 14:06:35 +08:00
services feat: add trial model list in system features (#31313) 2026-01-26 11:52:05 +08:00
tasks feat: archive workflow run logs backend (#31310) 2026-01-23 13:11:56 +08:00
templates refactor: make url in email template more better (#31166) 2026-01-19 14:28:41 +08:00
tests refactor: port AppModelConfig (#30919) 2026-01-24 10:25:51 +08:00
.dockerignore Enhance Code Consistency Across Repository with .editorconfig (#19023) 2025-04-29 18:04:33 +08:00
.env.example feat: add lock for retention jobs (#31320) 2026-01-21 14:06:35 +08:00
.importlinter refactor: Move workflow node factory to app workflow (#31385) 2026-01-22 18:08:21 +08:00
.ruff.toml refactor: split changes for api/services/tools/api_tools_manage_servi… (#29899) 2025-12-31 10:24:35 +08:00
AGENTS.md chore: Update coding agent workflow for backend (#31093) 2026-01-16 14:28:47 +08:00
app_factory.py feat: init fastopenapi (#30453) 2026-01-23 21:07:52 +09:00
app.py fix: determine cpu cores determination in baseedpyright-check script on macos (#28058) 2025-11-12 19:27:27 +08:00
celery_entrypoint.py chore(api): adjust monkey patching in gunicorn.conf.py (#26056) 2025-09-22 18:23:01 +08:00
cnt_base.sh add cnt script and one more example (#28272) 2025-11-18 16:44:14 +09:00
commands.py feat: archive workflow run logs backend (#31310) 2026-01-23 13:11:56 +08:00
dify_app.py
Dockerfile chore: Harden API image Node.js runtime install (#30497) 2026-01-05 21:19:26 +09:00
gunicorn.conf.py docs(api): update docs about gevent setup in app.py (#27611) 2025-10-30 15:43:08 +08:00
pyproject.toml fix(http-request): prevent UUID truncation in JSON body (#31444) 2026-01-24 10:21:21 +08:00
pyrefly.toml refactor: port reqparse to Pydantic model (#28949) 2025-12-05 13:05:53 +09:00
pyrightconfig.json feat: init fastopenapi (#30453) 2026-01-23 21:07:52 +09:00
pytest.ini test: Consolidate API CI test runner (#29440) 2025-12-15 13:20:31 +08:00
README.md chore: Update dev setup scripts and API README (#31415) 2026-01-24 10:20:47 +08:00
ty.toml chore: apply ty checks on api code with script and ci action (#24653) 2025-09-02 16:05:13 +08:00
uv.lock chore(deps): bump pyasn1 from 0.6.1 to 0.6.2 in /api (#31140) 2026-01-24 10:31:56 +08:00

Dify Backend API

Setup and Run

Important

In the v1.3.0 release, poetry has been replaced with uv as the package manager for Dify API backend service.

uv and pnpm are required to run the setup and development commands below.

The scripts resolve paths relative to their location, so you can run them from anywhere.

  1. Run setup (copies env files and installs dependencies).

    ./dev/setup
    
  2. Review api/.env, web/.env.local, and docker/middleware.env values (see the SECRET_KEY note below).

  3. Start middleware (PostgreSQL/Redis/Weaviate).

    ./dev/start-docker-compose
    
  4. Start backend (runs migrations first).

    ./dev/start-api
    
  5. Start Dify web service.

    ./dev/start-web
    
  6. Set up your application by visiting http://localhost:3000.

  7. Optional: start the worker service (async tasks, runs from api).

    ./dev/start-worker
    
  8. Optional: start Celery Beat (scheduled tasks).

    ./dev/start-beat
    

Manual commands

Show manual setup and run steps

These commands assume you start from the repository root.

  1. Start the docker-compose stack.

    The backend requires middleware, including PostgreSQL, Redis, and Weaviate, which can be started together using docker-compose.

    cp docker/middleware.env.example docker/middleware.env
    # Use mysql or another vector database profile if you are not using postgres/weaviate.
    docker compose -f docker/docker-compose.middleware.yaml --profile postgresql --profile weaviate -p dify up -d
    
  2. Copy env files.

    cp api/.env.example api/.env
    cp web/.env.example web/.env.local
    
  3. Install UV if needed.

    pip install uv
    # Or on macOS
    brew install uv
    
  4. Install API dependencies.

    cd api
    uv sync --group dev
    
  5. Install web dependencies.

    cd web
    pnpm install
    cd ..
    
  6. Start backend (runs migrations first, in a new terminal).

    cd api
    uv run flask db upgrade
    uv run flask run --host 0.0.0.0 --port=5001 --debug
    
  7. Start Dify web service (in a new terminal).

    cd web
    pnpm dev:inspect
    
  8. Set up your application by visiting http://localhost:3000.

  9. Optional: start the worker service (async tasks, in a new terminal).

    cd api
    uv run celery -A app.celery worker -P threads -c 2 --loglevel INFO -Q dataset,priority_dataset,priority_pipeline,pipeline,mail,ops_trace,app_deletion,plugin,workflow_storage,conversation,workflow,schedule_poller,schedule_executor,triggered_workflow_dispatcher,trigger_refresh_executor,retention
    
  10. Optional: start Celery Beat (scheduled tasks, in a new terminal).

    cd api
    uv run celery -A app.celery beat
    

Environment notes

Important

When the frontend and backend run on different subdomains, set COOKIE_DOMAIN to the sites top-level domain (e.g., example.com). The frontend and backend must be under the same top-level domain in order to share authentication cookies.

  • Generate a SECRET_KEY in the .env file.

    bash for Linux

    sed -i "/^SECRET_KEY=/c\\SECRET_KEY=$(openssl rand -base64 42)" .env
    

    bash for Mac

    secret_key=$(openssl rand -base64 42)
    sed -i '' "/^SECRET_KEY=/c\\
    SECRET_KEY=${secret_key}" .env
    

Testing

  1. Install dependencies for both the backend and the test environment

    cd api
    uv sync --group dev
    
  2. Run the tests locally with mocked system environment variables in tool.pytest_env section in pyproject.toml, more can check Claude.md

    cd api
    uv run pytest                           # Run all tests
    uv run pytest tests/unit_tests/         # Unit tests only
    uv run pytest tests/integration_tests/  # Integration tests
    
    # Code quality
    ./dev/reformat               # Run all formatters and linters
    uv run ruff check --fix ./   # Fix linting issues
    uv run ruff format ./        # Format code
    uv run basedpyright .        # Type checking