# API

Download VPNDetection's databases on your own schedule through the Database API, skip builds that haven't changed, and check each file's SHA-256.

The [Database API](/api/database/downloadDatabase) downloads any database your organization holds a license for, the Open ones included, with an API key carrying the `db.download` scope. It suits a job that runs on your own schedule, and it's what the [CLI](/integrations/cli) and the [client libraries](/integrations/languages) call.

A download answers with a `302` redirect to a time-limited link, so the bytes come straight from object storage. The link only has to be valid when the download starts, so a long download isn't cut off when it expires. To have new builds come to you instead of fetching them, use [Database Delivery](/integrations/database-delivery).

## Before you begin

- An API key with the `db.download` scope, which the console shows as **Download databases** among the key's **Permissions**. Your organization's `Default` key has it.
- A license for the database you want. Every organization holds the Open databases from the start.
- `curl`, and `sha256sum` to check the file, or `shasum -a 256` on macOS.

To keep the key out of your scripts, store it in an environment variable:

```bash
export VPNDETECTION_API_KEY="YOUR_API_KEY"
```

## 1. Find the database's ID

A download names a database by its ID, which carries its version, such as `vpn_ip_v1`. Each dataset's page in the [Database Reference](/database) shows its ID, and [List](/api/database/listDatabases) returns every database with your organization's `standing` for each, `licensed`, `expired` or `unlicensed`:

```bash
curl -H "Authorization: Bearer $VPNDETECTION_API_KEY" https://api.vpndetection.io/api/v1/database/list
```

Pass a version's `id` from that answer, never the family's base: `vpn_ip_v1`, not `vpn_ip`.

## 2. Check whether a new build is out

To avoid downloading a file you already have, ask for the database's [Metadata](/api/database/databaseMetadata) first. It needs no license, and its `updated` field is the current build's date and `entries` its row count:

```bash
curl -H "Authorization: Bearer $VPNDETECTION_API_KEY" "https://api.vpndetection.io/api/v1/database/metadata?id=vpn_ip_v1"
```

A daily job compares `updated` with the date of the copy it holds, and skips a database that hasn't changed.

## 3. Download the file

Ask for the database and its format, and let `curl` follow the redirect to the file with `-L`:

```bash
curl -L -o vpn_ip_v1.mmdb "https://api.vpndetection.io/api/v1/database/download?id=vpn_ip_v1&format=mmdb&apikey=$VPNDETECTION_API_KEY"
```

`format` is `csvgz` or `mmdb`, whichever the database is built in, as its page lists. [Formats](/database/formats) explains each one.

## 4. Check the file

[Checksums](/api/database/databaseChecksum) gives each file's SHA-256, among other digests, so you can check a copy once it lands:

```bash
curl -H "Authorization: Bearer $VPNDETECTION_API_KEY" "https://api.vpndetection.io/api/v1/database/checksum?id=vpn_ip_v1&format=mmdb"
```

Its `checksums.sha256` is the digest of the current build's file. Compute your copy's and compare the two:

```bash
sha256sum vpn_ip_v1.mmdb
```

The values match for an intact file. A mismatch means the file is truncated, corrupted or from a different build, so download it again.

## Verify that a download worked

[Download history](/api/database/listDownloads) lists every download your organization made, refusals included, each with its outcome and status. The console shows the same list under **Database** > **Download history**.

## When a download fails

A refused download answers with a status and a result code, `rc`, which says what to change:

| Status | `rc` | What to do |
| --- | --- | --- |
| `401` | `UNAUTHORIZED` | Send a valid key that carries the `db.download` scope. |
| `403` | `NOT_LICENSED` | Your organization holds no license for this database, so choose one it holds. |
| `403` | `LICENSE_EXPIRED` | Your organization's license for this database has ended, so renew it. |
| `403` | `KEY_OVER_HTTP` | Send the request to `https://`, never plain `http://`. |
| `404` | `UNKNOWN_DATASET` | Pass a version's ID, such as `vpn_ip_v1`, rather than the family's base. |
| `400` | `INVALID_FORMAT` | Ask for a format the database is built in. |
| `400` | `INVALID_BUILD` | Pass a build's name in `build`, or leave `build` out. |
| `410` | `BUILD_GONE` | The build named in `build` is no longer kept, so download without `build` for the live one. |
| `503` | `NOT_AVAILABLE` | Today's copy hasn't been published yet, so try again later. |

Builds are kept for 7 days, which is why a link that names an older build answers `410`. Which credential each endpoint takes is on [Authentication](/api/auth).

## Next steps

You can now download a database, skip builds you already have and check each file. To have new builds come to you instead, announce them to a [webhook](/integrations/webhooks), whose payload carries these same download links, or have them written to your own storage: [AWS S3](/integrations/aws-s3), [Google Cloud Storage](/integrations/google-cloud-storage), [Azure Blob Storage](/integrations/azure-blob-storage) or [SFTP](/integrations/sftp).
