Skip to navigation

Manage Routes with the API

Create routes and targets, shift traffic, and automate it from your own code.
View as Markdown

Everything you can do in the dashboard you can do over HTTP, so routes can be created by a deploy script, a signup flow, or a job that runs per customer. This guide uses curl and jq. You’ll need an API key (see Managing API Keys) and the CREDENTIAL variable from Authentication. Remember the rate limits.

API=https://api.divert.ly/v1
AUTH="Authorization: Key $CREDENTIAL"

1. Find your domain

curl -s $API/domains -H "$AUTH" | jq '.body[] | {id, name}'

Note the id of the domain the route will live under. Use dvt.fyi if you haven’t added your own. Then:

DOMAIN_ID=<the domain id>

2. Create a route

ROUTE_ID=$(curl -s -X POST $API/domains/$DOMAIN_ID/routes \
-H "$AUTH" -H "Content-Type: application/json" \
-d '{
"slug": "acme-download",
"nickname": "Acme download",
"description": "Latest installer",
"enabled": true,
"targetStrategy": "weighted",
"targetStrategyFallback": "resolve"
}' | jq -r '.body.id')

The slug is the path after the domain. On a shared domain such as dvt.fyi it must be unique across all organizations, so prefix it with something of your own. targetStrategy can be any strategy your plan includes, and strategies that read a header or query parameter also need targetStrategySelector. See Target Selection.

3. Add targets

A route needs at least one target before it resolves.

curl -s -X POST $API/domains/$DOMAIN_ID/routes/$ROUTE_ID/targets \
-H "$AUTH" -H "Content-Type: application/json" \
-d '{"url": "https://downloads.acme.com/v2/app.zip", "weight": 90, "description": "Current release"}'
NEW_TARGET_ID=$(curl -s -X POST $API/domains/$DOMAIN_ID/routes/$ROUTE_ID/targets \
-H "$AUTH" -H "Content-Type: application/json" \
-d '{"url": "https://downloads.acme.com/v3/app.zip", "weight": 10, "description": "Canary"}' \
| jq -r '.body.id')

Target URLs must use http or https. For non-weighted strategies, set selectorValue instead of weight. For example, "selectorValue": "US" on a Country route.

The link https://<your-domain>/acme-download is now live, and roughly 10% of visitors reach the canary.

4. Change weights

Update a target by sending only the fields you want to change:

curl -s -X PUT $API/domains/$DOMAIN_ID/routes/$ROUTE_ID/targets/$NEW_TARGET_ID \
-H "$AUTH" -H "Content-Type: application/json" \
-d '{"weight": 50}'

The change applies to the next request, with nothing to deploy. Ramp up the new target’s weight, and lower the old target’s to match, in whatever steps suit you. See Canary Rollout for the approach.

5. Roll back

Set the new target’s weight back to 0, which sends all traffic to the other target:

curl -s -X PUT $API/domains/$DOMAIN_ID/routes/$ROUTE_ID/targets/$NEW_TARGET_ID \
-H "$AUTH" -H "Content-Type: application/json" \
-d '{"weight": 0}'

Or take the target out of rotation entirely by disabling it:

curl -s -X PUT $API/domains/$DOMAIN_ID/routes/$ROUTE_ID/targets/$NEW_TARGET_ID \
-H "$AUTH" -H "Content-Type: application/json" \
-d '{"enabled": false}'

Set "enabled": true to bring it back. A disabled target keeps its URL and weight.

One route per customer

Because routes are cheap and unlimited, you can give every customer, tenant, or record its own link. Create the route when the customer is created, and store the returned id next to your own record so you can update it later:

create_customer_route() {
curl -s -X POST $API/domains/$DOMAIN_ID/routes \
-H "$AUTH" -H "Content-Type: application/json" \
-d "{\"slug\": \"acme-$1\", \"nickname\": \"Customer $1\", \"enabled\": true}" \
| jq -r '.body.id'
}

Add a target to each route, and when the customer’s destination changes, update that target’s url. The link you gave them never changes.

Clean up

curl -s -X DELETE $API/domains/$DOMAIN_ID/routes/$ROUTE_ID -H "$AUTH"

Deleting a route is permanent. To stop one from resolving without deleting it, update it with {"enabled": false}.

Next