camptocamp.org API
Interactive versions and more: https://c2corg.github.io/v6_api/.
Python, PostgreSQL/PostGIS, Elasticsearch and Redis all run in the containers defined in
.devcontainer/: nothing but a container engine is installed on your
machine. A manual setup, with Python on the host, also exists but is not
recommended.
- Docker (Docker Desktop, or Docker Engine with Compose v2), or Podman (see Podman).
- Optional: VS Code with the Dev Containers extension.
On Apple Silicon, the published database and Elasticsearch images run under amd64 emulation (the Python container is native): it works, the index rebuild is just slower.
git clone https://github.com/c2corg/v6_api && code v6_api- Click Reopen in Container (or command palette →
Dev Containers: Reopen in Container). - Wait for
.devcontainer/setup.sh(thepostCreateCommand) to finish: a few minutes the first time, up to ~10 minutes if the search index has to be rebuilt under emulation. - In a VS Code terminal (it runs in the app container):
make serve, then open http://localhost:6543/health.
The same stack from any terminal. Compose does not run the postCreateCommand, so run the setup
yourself. A shell alias keeps the commands short:
git clone https://github.com/c2corg/v6_api && cd v6_api
alias dc='docker compose -p v6-api-dev -f .devcontainer/docker-compose.yml' # or podman compose
dc up -d --build --wait # build the app image, start and wait for the services
dc exec app bash .devcontainer/setup.sh # once, and again after a rebuild
dc exec app make serve # foreground; then http://localhost:6543/health
dc exec app bash # a shell in the app container, for the commands below
dc stop # stop, keeping the dataIt installs the locked Python dependencies (in /opt/venv, outside the workspace) and the git
pre-commit hook, generates the *.ini files, migrates the dev database to the alembic revision of
your checkout, creates the test database, and keeps the prebuilt search index if it matches the
indexing code of your checkout (otherwise rebuilds it). It is idempotent: re-run it after pulling
or switching branches; it keeps your data.
Unless stated otherwise, the make, pytest, alembic… commands of this README run inside the
app container (a VS Code terminal, or dc exec app bash). Do not run make bootstrap or
make start there: they belong to the manual setup.
| Service | Hostname (from the app container) | From the host |
|---|---|---|
| API | localhost:6543 |
127.0.0.1:6543 (once make serve runs) |
| PostgreSQL | postgresql:5432 |
forwarded by VS Code only |
| Elasticsearch | elasticsearch:9200 |
forwarded by VS Code only |
| Redis | redis:6379 |
forwarded by VS Code only |
- Dependencies or
.devcontainer/Dockerfilechanged:Dev Containers: Rebuild Containerin VS Code, ordc up -d --build --waitthendc exec app bash .devcontainer/setup.sh. - Fresh database and index (deletes your local data):
dc down -v && dc pull, then start and run the setup again. With VS Code the Compose project is named after the folder: find its…_postgres_dataand…_es_datavolumes withdocker volume ls.
See docs/claude-code-cloud.md (scripts/sandbox-up.sh).
Use a Compose provider supporting up --wait (Docker Compose v2 or podman-compose 1.6+; check with
podman compose version, choose one with PODMAN_COMPOSE_PROVIDER).
- macOS:
podman machine start(podman machine initfirst if there is no machine). - Linux, rootless:
systemctl --user enable --now podman.socket. - VS Code: set Dev Containers: Docker Path to
podmanand Dev Containers: Docker Compose Path to your provider, e.g.podman-compose. - Without VS Code:
alias dc='podman compose -p v6-api-dev -f .devcontainer/docker-compose.yml'.
- Do not edit the generated
*.inifiles. Put your overrides inconfig/env.local(git-ignored) and runmake load-env. In the dev container,config/env.devcontaineris applied last, so the service hostnames always point to the containers. - The dev database is a lightweight anonymized extract of production (about 45,000 documents), and
Elasticsearch comes with its index already built: see
scripts/dev-database. - Accounts:
moderator,user,blocked,unvalidated, passwordc2cfor all. - The UI dev servers (
npm run servein c2c_ui,http://localhost:8080or any other port oflocalhost/127.0.0.1) are allowed by the CORS configuration of the local API: selecthttp://localhost:6543as their backend. - There is no Discourse:
config/env.devsetsdiscourse_enabled="false". Logging in, registering and editing a profile work; creating the comments topic of a document answers 503. To work on the forum, run a Discourse and setdiscourse_enabled="true",discourse_url, its key and secret inconfig/env.local. The tests mock the forum and do not need it. - Dependencies: edit
pyproject.toml, runuv lock, commit both files, then re-run the setup.
For the "Mobilité douce" (public transport) features, see SMART-ORIGIN-README.md.
make serve # the API, with auto-reload, on port 6543
make run-syncer # Elasticsearch syncer: needed to see your edits in the search
make run-background-jobs # scheduled jobsEach one stays in the foreground: use one terminal per command. Check http://localhost:6543/health.
The API is a FastAPI application, served by uvicorn: c2corg_api/asgi.py reads the .ini file named by C2C_API_CONFIG (production.ini by default). In production, gunicorn runs uvicorn workers (gunicorn -k uvicorn_worker.UvicornWorker c2corg_api.asgi:app).
c2corg_api.config reads the settings of the .ini files, for the application and the scripts. The tests drive the application through a WSGI bridge (c2corg_api:main).
c2corg_api/app.py: the FastAPI application, its middlewares (CORS, token validation, unexpected errors) and the answers to unknown routes and methods.c2corg_api/web/: the routes, one module per resource (documents.py,users.py,navitia.py…). They are thin: each one runs a function ofviews/throughweb/handlers.py(the order of the checks, the commit of the writes);web/request.pygives the views the request they expect (validated,errors,matchdict…),web/checks.pychecks the content type, the permission and the origin, andweb/answers.pyturns what the views return or raise into the answer.c2corg_api/views/: the view functions and their validators (views/validation.py).@write(...)inviews/resource.pydeclares the validators, permission and content type of a write view; the read routes declare theirs inweb/. The route checks the rate limiting (writes only), then the content type, the permission and the validators, and calls the view.c2corg_api/schemas/: the Pydantic schemas of the request bodies;c2corg_api/models/: the SQLAlchemy models.c2corg_api/security/: tokens (roles.py), rate limiting, Discourse client;c2corg_api/httpexceptions.py: the HTTP errors the views raise.c2corg_api/scripts/: the Elasticsearch syncer, the job scheduler and the maintenance scripts; they read the.inifile withc2corg_api/config.py.
make test # pytest -n 4
pytest c2corg_api/tests/models/test_book.py # one file- With
-n, each pytest-xdist worker uses its own database (<test database>_gw0,_gw1...), Elasticsearch index and Redis names, created on the first run. The test database user needs the right to create databases. - Write new tests as plain functions asking for the fixtures of
c2corg_api/tests/conftest.py(client,session,user_ids,tokens,settings...) and the helpers ofc2corg_api.tests(auth_headers,send_json...); the shared document scenarios are inc2corg_api/tests/views/scenarios.py. See #2610. - A warning fails the tests (
filterwarningsinpyproject.toml): fix its cause rather than ignoring it. The only exceptions are the warnings of the Elasticsearch 2 client, still shown until the move to Elasticsearch 8.
c2corg_api/tests/contract/ checks what the clients of the API rely on, so that a change of framework or library cannot change it unnoticed:
routes.txtlists every route with its methods and permission. After adding, removing or changing a route, regenerate it withpython -m c2corg_api.tests.contract.inventory.- Each contract test sends a request and compares the status, the headers clients rely on (CORS,
Cache-Control,ETag…) and the shape of the JSON body (keys and value types) with a snapshot insnapshots/. After an intended change of the API, rewrite the snapshots withCONTRACT_UPDATE=1 pytest c2corg_api/tests/contractand review their diff. - Every route has a contract test (marked with
@contract(...));pending.txt, empty, would list a route without one. A new route needs its contract test.
scripts/loadtest.py replays a mix of read requests (documents, listings, search, history) with a few threads and prints throughput and latency percentiles per kind of request. It compares two versions of the API on the same machine and data; run each version twice to see the noise:
python scripts/loadtest.py http://localhost:6543 --threads 8 --duration 60For load tests of a staging environment, use the Locust scenarios in c2corg_api/scripts/loadtests/.
make format # ruff format + ruff check --fix (VS Code also formats on save in the dev container)
make lint # ruff check + ruff format --check, as in CI
make audit # known vulnerabilities of the locked dependencies (exceptions: AUDIT_IGNORE in the Makefile)A git pre-commit hook runs ruff before each commit (installed by the setup). It uses the Python of the
app container: commit from the dev container (VS Code does), or install pre-commit on the host too
(uv tool install pre-commit or brew install pre-commit) to commit from a host terminal.
- Flush the Redis cache:
make flush-redis. - Rebuild the whole index without interrupting the search (production):
reindex_es production.ini. A new timestamped index is built next to the live one, thenelasticsearch.indexis switched to it as an alias, atomically. The first time, if it is still a real index, run once with--replace-index(short search gap).--keep-oldkeeps the previous index instead of deleting it. - Compute again the quality ("complétude") of the outings, after the rules of the UI changed (see
c2corg_api/scripts/outings/README.md):
recompute_outing_quality development.ini --dry-run.
| Component | master (this branch) |
Work in progress |
|---|---|---|
| Python | 3.15, dependencies managed with uv | |
| Web framework | FastAPI, served by uvicorn | |
| PostgreSQL | Dev: PostgreSQL 18 + PostGIS 3.6, image ghcr.io/c2corg/c2corg-dev-db (anonymized extract) |
Production servers to PostgreSQL 18 + PostGIS 3.6: branch pg18-postgis36-migration of the infra repository |
| Elasticsearch | dev image ghcr.io/c2corg/c2corg-dev-es with the index built |
8.19: branch feat/elasticsearch-8, not merged |
| Redis | 7.2 |
One source for the dev services. The dev container (.devcontainer/docker-compose.yml) and the manual setup
(docker-compose.yml) use the same two images, published together with the same tag by
scripts/dev-database/publish.sh. Changing the version of PostgreSQL or
Elasticsearch is done there (scripts/dev-database/Dockerfile*), followed by a new publication: both setups then
follow, with no Dockerfile of their own. Until the Elasticsearch 8 images are published, feat/elasticsearch-8 has
to build its index locally (make init-elastic).


