> ## 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

> Calculate the routes and distances of a transport chain, with the resolved locations, airports, harbors, and rail yards.

The Distance API calculates the routes and distances of a [transport chain](https://docs.terratwin.com/content/guides/transport-chains.md) – by road, rail, water, and air – without calculating emissions.
It returns the distance of each operation and of the whole chain, the locations TerraTwin resolved, including the airports, harbors, and rail yards it chose, and optionally each route as a polyline.

New to the Distance API? Start with the [Distance API quick start](https://docs.terratwin.com/content/get-started/distance-api.md).

## Endpoint

| Endpoint | What it does |
| - | - |
| [`POST /routes/calculate`](https://docs.terratwin.com/api-reference/calculate-transport-routes) | Calculates the routes and distances of a transport chain. |

The endpoint is served from `https://api.terratwin.com/api/v1/transportation` and needs your API key in the `x-api-key` header.

## Same routing as the Emissions API

A Distance API request is routed exactly as the [Emissions API](https://docs.terratwin.com/content/guides/emissions-api.md) routes the same request: the same location matching, transport modes, fallback modes, airports, stopovers, harbors, ferries, and rail yards.
So the routes and distances it returns are the ones an emissions calculation of that shipment is based on.

> [!NOTE]
>
> The Distance API returns the routed distances as they are. The Emissions API applies distance adjustment factors to some modes when calculating emissions, so the distance used in an emissions calculation can be slightly longer than the Distance API's.

## The request

The request has the same structure as an emissions request:

| Property | Purpose | Guide |
| - | - | - |
| `origin`, `destination` | Where the journey starts and finishes. | [Locations](https://docs.terratwin.com/content/guides/locations.md) |
| `transportChain` | The ordered transport operations, with a `HUB` between each pair. | [Transport chains](https://docs.terratwin.com/content/guides/transport-chains.md) |
| `cargo` | Optional. Only `cargo.category` is used – it steers the default vessel type for `WATER` operations. It defaults to `GENERAL`. | [Cargo](https://docs.terratwin.com/content/guides/cargo.md) |
| `options` | See below. | |
| `id`, `context` | Your own identifier and data, returned unchanged in the response. | |

Each transport operation is one of [`ROAD`](https://docs.terratwin.com/content/guides/road-transport), [`RAIL`](https://docs.terratwin.com/content/guides/rail-transport), [`WATER`](https://docs.terratwin.com/content/guides/water-transport), [`AIR`](https://docs.terratwin.com/content/guides/air-transport), or [`UNKNOWN`](https://docs.terratwin.com/content/guides/unknown-transport).
Operation properties that affect the route – such as the vessel and navigation constraints of a `WATER` leg, or the aircraft of an `AIR` leg – work as described in each mode's guide.

Compared with the Emissions API, a Distance API request doesn't take:

* `WEIGHTED_MODES` operations
* `firstMile` and `lastMile` on `AIR`, `RAIL`, and `WATER` operations – connecting road journeys are still routed and included in the distances
* `hubType` on a `HUB` – a hub only needs a `type` and a `location`, and marks where one leg ends and the next begins
* The emissions-only options `storeResult`, `hubInsertion`, and `defaultHubTypes`

### Options

| Option | What it does |
| - | - |
| `fallbackTransportModes` | The modes tried, in order, for `UNKNOWN` operations and for operations that can't be routed with the requested mode. |
| `includeRoutePolylines` | Adds each route as an encoded polyline (Google Encoded Polyline Algorithm Format) to `ROAD`, `RAIL`, and `WATER` results. |

## The response

| Property | What it tells you |
| - | - |
| `origin`, `destination` | The resolved locations, with coordinates and a formatted address. |
| `distanceKm` | The total routed distance of the chain, in kilometres. |
| `transportChain` | One result per requested leg, in order. |
| `creditCost` | The Distance API credits the request consumed. |

Each leg in `transportChain` has:

* `type` – the mode of the leg. For an `UNKNOWN` operation, it's the fallback mode that was used.
* `start` and `end` – the resolved locations where the leg begins and ends.
* `distanceKm` – the distance of the leg, including its connecting road journeys.
* `elements` – the operations that make up the leg, in order. A `WATER` leg from an address, for example, is a `ROAD` journey to the departure harbor followed by the `WATER` voyage. `AIR` elements show the aircraft, and `WATER` elements the vessel and its capacity band.
* `subOperations` on a `ROAD` element – roll-on/roll-off ferry (`WATER`) and train (`RAIL`) crossings, such as the Channel Tunnel shuttle.

Results aren't stored, so there is no endpoint to retrieve them later.

## Credits

Distance API requests are charged to your account's **Distance API credits**, counted in the same way as for the Emissions API – one credit per transport operation – see [Credits & usage](https://docs.terratwin.com/content/guides/credits.md).

If your account has no Distance API credits left, requests are rejected with `403` and error code 42, *"Your account has no remaining route credits"*. Emissions API credits are never used for Distance API requests.

## Related

> [card] [Emissions API](https://docs.terratwin.com/content/guides/emissions-api.md)
>
> Need the emissions of the journey as well? The Emissions API takes the same transport chain and calculates its emissions.

> [card] [API reference](https://docs.terratwin.com/api-reference/calculate-transport-routes.md)
>
> Every request field, response object, and error code of the Distance API.


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