# api

> Start the datacontract CLI as server application with REST API.

# `datacontract api`

Start the datacontract CLI as server application with REST API. The OpenAPI documentation as Swagger UI is available on http://localhost:4242. You can execute the commands directly from the Swagger UI. To protect the API, you can set the environment variable DATACONTRACT_CLI_API_KEY to a secret API key. To authenticate, requests must include the header 'x-api-key' with the correct API key. This is highly recommended, as data contract tests may be subject to SQL injections or leak sensitive information. To connect to servers (such as a Snowflake data source), set the credentials as environment variables as documented in https://cli.datacontract.com/configuration It is possible to run the API with extra arguments for `uvicorn.run()` as keyword arguments, e.g.: `datacontract api --port 1234 --root_path /datacontract`.

```bash
datacontract api [OPTIONS]
```

| Option | Default | Description |
|---|---|---|
| `--port` | `4242` | Bind socket to this port. |
| `--host` | `127.0.0.1` | Bind socket to this host. Hint: For running in docker, set it to 0.0.0.0 |
| `--reload` / `--no-reload` | `--no-reload` | Watch the source files and restart the server on changes. For development only; off by default. |
| `--contract-variables` | — | Environment variables a posted data contract may read through $\{VAR\} references, as comma-separated fnmatch globs (e.g. 'TABLE_*,CUTOFF_DATE', or '*' to allow all). Empty by default. |
| `--allow-local-files` / `--no-allow-local-files` | — | Let a posted data contract read the server's own disk through servers[].type: local. Off by default. |
| `--allow-private-network` / `--no-allow-private-network` | — | Let a posted data contract's API requests (servers[].type: api) reach private network addresses, such as localhost or the cloud metadata endpoint. Off by default. |
| `--debug` / `--no-debug` | — | Enable debug logging |

```bash
datacontract api --port 4242 --host 0.0.0.0
```

Guide: **[Run as a web server](https://cli.datacontract.com/api.md)**.
