> ## Documentation Index
> Fetch the complete documentation index at: https://docs.terratwin.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Distance API quick start

> Build your first Distance API request step by step, then extend it to multimodal transport chains.

The [Distance API](https://docs.terratwin.com/content/guides/distance-api.md) calculates the routes and distances of a transport chain, without calculating emissions.
This tutorial builds a complete request one part at a time.
By the end, you will have calculated the road distance from Berlin to London, and then the distance of a journey by sea and road.

![](https://mintcdn.com/terratwin/0hbUXmLqgbSI5-VO/images/quick-start/berlin-to-london-light.png?fit=max\&auto=format\&n=0hbUXmLqgbSI5-VO\&q=85\&s=b3ab5d2343c060e130fbad1bf647f49f)

Road transport from the Reichstag Building in Berlin to the Palace of Westminster in London

> [card] 1. Set the locations
>
> Define where the journey starts and finishes.

> [card] 2. Build the chain
>
> Describe the ordered journey between those locations.

> [card] 3. Choose the operation
>
> Select how the cargo moves on each leg of the journey.

> [card] 4. Read the distances
>
> See the distance of each leg and of the whole journey.

## Before you begin

You need:

* A TerraTwin account
* An API key from **[Manage API Keys](https://www.terratwin.com/account/settings/api-key-manager)**
* At least one Distance API credit
* A way to send an HTTP request; this tutorial uses `curl` for the final test

If you haven't used TerraTwin before, start with the [Quick start](https://docs.terratwin.com/content/get-started/quick-start.md), which covers what all TerraTwin APIs have in common.

> [card] Authentication
>
> Every request must include your API key in the `x-api-key` header.
>
> See [API keys](https://docs.terratwin.com/content/guides/authentication.md) for creating, storing, rotating, and revoking keys.

> [card] Credits
>
> Requests consume Distance API credits according to the transport operations that TerraTwin routes.
>
> See [Credits & usage](https://docs.terratwin.com/content/guides/credits.md) for charging rules and examples.

## Build the request

We will build the request body one section at a time. Each step shows the complete JSON as it stands at that point.

**1. Define the origin and destination**

Add top level `origin` and `destination` objects.
Each location needs a `type` and enough information for TerraTwin to resolve it.

For this tutorial, use structured address `PLACE` locations:

```json theme={null}
{
  "origin": {
    "type": "PLACE",
    "street": "Platz der Republik 1",
    "postcode": "11011",
    "city": "Berlin",
    "country": "DE"
  },
  "destination": {
    "type": "PLACE",
    "postcode": "SW1A 0AA",
    "city": "London",
    "country": "GB"
  }
}
```

`PLACE` is suitable for cities, addresses, postcodes, buildings, or coordinates. TerraTwin can also resolve purpose specific locations such as `AIRPORT`, `RAIL_YARD`, and `HARBOR`.

See the [Locations guide](https://docs.terratwin.com/content/guides/locations.md) for coordinates, structured and freeform addresses, postcodes, IATA airport codes, UN/LOCODEs, location types, and matching behavior.

**2. Create the transport chain**

Add a `transportChain` array to describe how the cargo moves from the origin to the destination.

A transport chain is ordered. Each element is either:

* A **transport operation**, which moves the cargo on one requested leg
* A **`HUB`**, which marks where one leg ends and the next begins

This journey has only one leg, so the chain contains one road operation:

```json theme={null}
{
  "origin": {
    "type": "PLACE",
    "street": "Platz der Republik 1",
    "postcode": "11011",
    "city": "Berlin",
    "country": "DE"
  },
  "destination": {
    "type": "PLACE",
    "postcode": "SW1A 0AA",
    "city": "London",
    "country": "GB"
  },
  "transportChain": [
    {
      "type": "ROAD"
    }
  ]
}
```

See the [Transport chains guide](https://docs.terratwin.com/content/guides/transport-chains.md) for single leg and multimodal chains, chain boundaries, and the difference between requested legs and the operations realized in the response.

**3. Choose the transport operation**

The `type` inside the transport chain element selects the mode for that leg: `ROAD`, `RAIL`, `WATER`, `AIR`, or `UNKNOWN` when the mode isn't known.
This tutorial uses `ROAD`, which lets TerraTwin choose a default vehicle.

Operation properties that affect the route – such as the vessel of a `WATER` leg, which decides the harbors and waterways it can use, or the aircraft of an `AIR` leg – work as they do in the Emissions API. See the guide for each mode:

* [Road transport](https://docs.terratwin.com/content/guides/road-transport.md)
* [Rail transport](https://docs.terratwin.com/content/guides/rail-transport.md)
* [Water transport](https://docs.terratwin.com/content/guides/water-transport.md)
* [Air transport](https://docs.terratwin.com/content/guides/air-transport.md)
* [Unknown transport](https://docs.terratwin.com/content/guides/unknown-transport.md)

> [!NOTE]
>
> The Distance API doesn't accept `WEIGHTED_MODES` operations, or the `firstMile` and `lastMile` properties. Connecting road journeys, for example from an address to a harbor, are still routed and included in the distances.

## Send the request

Replace `your-api-key` with your TerraTwin API key, then run the command for your shell.

> [tab] PowerShell 7.3+
>
> ```powershell theme={null}
> curl.exe --silent --show-error --fail-with-body `
>   --request POST `
>   --url "https://api.terratwin.com/api/v1/transportation/routes/calculate" `
>   --header "Content-Type: application/json" `
>   --header "x-api-key: your-api-key" `
>   --data-raw '{
>     "origin": {
>       "type": "PLACE",
>       "street": "Platz der Republik 1",
>       "postcode": "11011",
>       "city": "Berlin",
>       "country": "DE"
>     },
>     "destination": {
>       "type": "PLACE",
>       "postcode": "SW1A 0AA",
>       "city": "London",
>       "country": "GB"
>     },
>     "transportChain": [
>       {
>         "type": "ROAD"
>       }
>     ]
>   }'
> ```

> [tab] Windows PowerShell 5.1
>
> ```powershell theme={null}
> curl.exe --% --silent --show-error --fail-with-body --request POST --url "https://api.terratwin.com/api/v1/transportation/routes/calculate" --header "Content-Type: application/json" --header "x-api-key: your-api-key" --data-raw "{\"origin\":{\"type\":\"PLACE\",\"street\":\"Platz der Republik 1\",\"postcode\":\"11011\",\"city\":\"Berlin\",\"country\":\"DE\"},\"destination\":{\"type\":\"PLACE\",\"postcode\":\"SW1A 0AA\",\"city\":\"London\",\"country\":\"GB\"},\"transportChain\":[{\"type\":\"ROAD\"}]}"
> ```

> [tab] Command Prompt
>
> ```bat theme={null}
> curl.exe --silent --show-error --fail-with-body ^
>   --request POST ^
>   --url "https://api.terratwin.com/api/v1/transportation/routes/calculate" ^
>   --header "Content-Type: application/json" ^
>   --header "x-api-key: your-api-key" ^
>   --data-raw "{\"origin\":{\"type\":\"PLACE\",\"street\":\"Platz der Republik 1\",\"postcode\":\"11011\",\"city\":\"Berlin\",\"country\":\"DE\"},\"destination\":{\"type\":\"PLACE\",\"postcode\":\"SW1A 0AA\",\"city\":\"London\",\"country\":\"GB\"},\"transportChain\":[{\"type\":\"ROAD\"}]}"
> ```

> [tab] Bash or zsh
>
> ```bash theme={null}
> curl --silent --show-error --fail-with-body \
>   --request POST \
>   --url 'https://api.terratwin.com/api/v1/transportation/routes/calculate' \
>   --header 'Content-Type: application/json' \
>   --header 'x-api-key: your-api-key' \
>   --data-raw '{
>     "origin": {
>       "type": "PLACE",
>       "street": "Platz der Republik 1",
>       "postcode": "11011",
>       "city": "Berlin",
>       "country": "DE"
>     },
>     "destination": {
>       "type": "PLACE",
>       "postcode": "SW1A 0AA",
>       "city": "London",
>       "country": "GB"
>     },
>     "transportChain": [
>       {
>         "type": "ROAD"
>       }
>     ]
>   }'
> ```

> [!WARNING]
>
> Treat your API key as a secret. The inline value is convenient for this first test, but do not commit a real key to source control or share it in logs, screenshots, or support messages.

A successful request returns HTTP `200` and a JSON result.

## Read the result

The response starts with the resolved locations and the total distance:

```json theme={null}
{
  "origin": {
    "type": "PLACE",
    "countryCode": "DE",
    "latitude": 52.5185941,
    "longitude": 13.3762818,
    "postcode": "10557",
    "city": "Berlin",
    "street": "Platz der Republik 1",
    "name": "Deutscher Bundestag",
    "formatted": "Deutscher Bundestag\nPlatz der Republik 1\n10557 Berlin\nGermany"
  },
  "destination": {
    "type": "PLACE",
    "countryCode": "GB",
    "latitude": 51.4998,
    "longitude": -0.1247,
    "postcode": "SW1A 0AA",
    "city": "City of Westminster",
    "formatted": "City of Westminster\nSW1A 0AA\nUnited Kingdom of Great Britain and Northern Ireland"
  },
  "distanceKm": 1095.458186875761,
  "transportChain": [ ... ],
  "creditCost": 1
}
```

| Property | What it tells you |
| - | - |
| `origin`, `destination` | The locations TerraTwin resolved from the request, with coordinates and a formatted address. |
| `distanceKm` | The total routed distance of the transport chain, in kilometres. |
| `transportChain` | One result per requested leg, in order, each with its own `distanceKm` and the `elements` that make it up. |
| `creditCost` | Distance API credits consumed by the request. |

Each leg's `elements` are the operations TerraTwin actually routed. A road journey can include roll-on/roll-off crossings as `subOperations` – this one crosses the Channel on the Eurotunnel shuttle:

| Sub-operation | From | To | Distance |
| - | - | - | -: |
| `ROAD` | Berlin | Coquelles Terminal, France | 929.5 km |
| `RAIL` | Coquelles Terminal | Folkestone Terminal, UK | 58.4 km |
| `ROAD` | Folkestone Terminal | London | 107.6 km |

> [!TIP]
>
> Set `options.includeRoutePolylines` to `true` to also get each route as an encoded polyline, for drawing it on a map.

For every request field, response schema, and error model, see the [Distance API reference](https://docs.terratwin.com/api-reference/calculate-transport-routes.md).

## Extend the request to multiple legs

A chain can contain several transport operations, but each pair of requested operations must be separated by a `HUB` that defines where the first leg ends and the next begins.

This chain moves cargo by water from the port of London Gateway \[`GBSHV`] to the port of Antwerp \[`BEANR`], and then by road to Frankfurt:

```json theme={null}
{
  "origin": {
    "type": "HARBOR",
    "unLoCode": "GBSHV"
  },
  "destination": {
    "type": "PLACE",
    "address": "Römerberg 27, 60311 Frankfurt am Main",
    "country": "DE"
  },
  "transportChain": [
    {
      "type": "WATER"
    }, {
      "type": "HUB",
      "location": {
        "type": "HARBOR",
        "unLoCode": "BEANR"
      }
    }, {
      "type": "ROAD"
    }
  ]
}
```

![](https://mintcdn.com/terratwin/0hbUXmLqgbSI5-VO/images/quick-start/london-to-frankfurt-light.png?fit=max\&auto=format\&n=0hbUXmLqgbSI5-VO\&q=85\&s=fa90bc7bab2274c0b393cc02ee42b994)

Sea transport from London Gateway to the Port of Antwerp, then by road to Frankfurt’s Römer

The response has one result per leg:

| Leg | From | To | Distance |
| - | - | - | -: |
| `WATER` | Port of London Gateway | Port of Antwerp | 297.2 km |
| `ROAD` | Port of Antwerp | Frankfurt | 402.7 km |

The total `distanceKm` is 699.9 km, and the request costs 2 credits, one for each leg.

> [!NOTE]
>
> In the Distance API, a `HUB` only needs a `type` and a `location`. It marks the boundary between legs and adds no distance of its own.

## Next steps

> [card] Distance API
>
> * [Distance API guide](https://docs.terratwin.com/content/guides/distance-api.md) – options, the response, and how distances are measured
> * [Credits & usage](https://docs.terratwin.com/content/guides/credits.md)

> [card] Shipment structure
>
> * [Locations](https://docs.terratwin.com/content/guides/locations.md)
> * [Transport chains](https://docs.terratwin.com/content/guides/transport-chains.md)
> * [Hubs](https://docs.terratwin.com/content/guides/hubs.md)

> [card] Transport operations
>
> * [Road transport](https://docs.terratwin.com/content/guides/road-transport.md)
> * [Rail transport](https://docs.terratwin.com/content/guides/rail-transport.md)
> * [Water transport](https://docs.terratwin.com/content/guides/water-transport.md)
> * [Air transport](https://docs.terratwin.com/content/guides/air-transport.md)
> * [Unknown transport](https://docs.terratwin.com/content/guides/unknown-transport.md)

> [card] Complete schema
>
> Use the [Distance API reference](https://docs.terratwin.com/api-reference/calculate-transport-routes.md) for exact field constraints, allowed values, complete response objects, and error codes.

> [!TIP]
>
> Need the emissions of the journey as well? The [Emissions API quick start](https://docs.terratwin.com/content/get-started/emissions-api.md) uses the same request structure.

If a request fails, see [Errors and troubleshooting](https://docs.terratwin.com/content/get-started/quick-start.md#errors-and-troubleshooting).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.