Skip to main content
The Distance API 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.

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

1. Set the locations

Define where the journey starts and finishes.

2. Build the chain

Describe the ordered journey between those locations.

3. Choose the operation

Select how the cargo moves on each leg of the journey.

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
  • 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, which covers what all TerraTwin APIs have in common.

Authentication

Every request must include your API key in the x-api-key header.See API keys for creating, storing, rotating, and revoking keys.

Credits

Requests consume Distance API credits according to the transport operations that TerraTwin routes.See Credits & usage 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:
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 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:
See the Transport chains guide 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:
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.
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:
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:
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.

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:

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: The total distanceKm is 699.9 km, and the request costs 2 credits, one for each leg.
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

Distance API

Shipment structure

Complete schema

Use the Distance API reference for exact field constraints, allowed values, complete response objects, and error codes.
Need the emissions of the journey as well? The Emissions API quick start uses the same request structure.
If a request fails, see Errors and troubleshooting.