You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tailscale/api.md

1742 lines
52 KiB
Markdown

# Tailscale API
The Tailscale API is a (mostly) RESTful API. Typically, both `POST` bodies and responses are JSON-encoded.
## Base URL
The base URL for the Tailscale API is `https://api.tailscale.com/api/v2/`.
3 years ago
Examples in this document may abbreviate this to `/api/v2/`.
## Authentication
Requests to the Tailscale API are authenticated with an API access token (sometimes called an API key).
Access tokens can be supplied as the username portion of HTTP Basic authentication (leave the password blank) or as an OAuth Bearer token:
``` sh
# passing token with basic auth
curl -u "tskey-api-xxxxx:" https://api.tailscale.com/api/v2/...
# passing token as bearer token
curl -H "Authorization: Bearer tskey-api-xxxxx" https://api.tailscale.com/api/v2/...
```
Access tokens for individual users can be created and managed from the [**Keys**](https://login.tailscale.com/admin/settings/keys) page of the admin console.
These tokens will have the same permissions as the owning user, and can be set to expire in 1 to 90 days.
Access tokens are identifiable by the prefix `tskey-api-`.
Alternatively, an OAuth client can be used to create short-lived access tokens with scoped permission.
OAuth clients don't expire, and can therefore be used to provide ongoing access to the API, creating access tokens as needed.
OAuth clients and the access tokens they create are not tied to an individual Tailscale user.
OAuth client secrets are identifiable by the prefix `tskey-client-`.
Learn more about [OAuth clients](https://tailscale.com/kb/1215/).
## Errors
The Tailscale API returns status codes consistent with [standard HTTP conventions](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status).
In addition to the status code, errors may include additional information in the response body:
``` jsonc
{
"message": "additional error information"
}
```
## Pagination
The Tailscale API does not currently support pagination. All results are returned at once.
# APIs
**[Device](#device)**
- Get a device: [`GET /api/v2/device/{deviceid}`](#get-device)
- Delete a device: [`DELETE /api/v2/device/{deviceID}`](#delete-device)
- **Routes**
- Get device routes: [`GET /api/v2/device/{deviceID}/routes`](#get-device-routes)
- Set device routes: [`POST /api/v2/device/{deviceID}/routes`](#set-device-routes)
- **Authorize**
- Authorize a device: [`POST /api/v2/device/{deviceID}/authorized`](#authorize-device)
- **Tags**
- Update tags: [`POST /api/v2/device/{deviceID}/tags`](#update-device-tags)
- **Key**
- Update device key: [`POST /api/v2/device/{deviceID}/key`](#update-device-key)
**[Tailnet](#tailnet)**
- [**Policy File**](#policy-file)
- Get policy file: [`GET /api/v2/tailnet/{tailnet}/acl`](#get-policy-file)
- Update policy file: [`POST /api/v2/tailnet/{tailnet}/acl`](#update-policy-file)
- Preview rule matches: [`POST /api/v2/tailnet/{tailnet}/acl/preview`](#preview-policy-file-rule-matches)
- Validate and test policy file: [`POST /api/v2/tailnet/{tailnet}/acl/validate`](#validate-and-test-policy-file)
- Devices
- List tailnet devices: [`GET /api/v2/tailnet/{tailnet}/devices`](#list-tailnet-devices)
- [**Keys**](#tailnet-keys)
- List tailnet keys: [`GET /api/v2/tailnet/{tailnet}/keys`](#list-tailnet-keys)
- Create an auth key: [`POST /api/v2/tailnet/{tailnet}/keys`](#create-auth-key)
- Get a key: [`GET /api/v2/tailnet/{tailnet}/keys/{keyid}`](#get-key)
- Delete a key: [`DELETE /api/v2/tailnet/{tailnet}/keys/{keyid}`](#delete-key)
- [**DNS**](#dns)
- **Nameservers**
- Get nameservers: [`GET /api/v2/tailnet/{tailnet}/dns/nameservers`](#get-nameservers)
- Set nameservers: [`POST /api/v2/tailnet/{tailnet}/dns/nameservers`](#set-nameservers)
- **Preferences**
- Get DNS preferences: [`GET /api/v2/tailnet/{tailnet}/dns/preferences`](#get-dns-preferences)
- Set DNS preferences: [`POST /api/v2/tailnet/{tailnet}/dns/preferences`](#set-dns-preferences)
- **Search paths**
- Get search paths: [`GET /api/v2/tailnet/{tailnet}/dns/searchpaths](#get-search-paths)
- Set search paths: [`POST /api/v2/tailnet/{tailnet}/dns/searchpaths`](#set-search-paths)
# Device
A Tailscale device (sometimes referred to as _node_ or _machine_), is any computer or mobile device that joins a tailnet.
Each device has a unique ID (`nodeId` in the JSON below) that is used to identify the device in API calls.
This ID can be found by going to the [**Machines**](https://login.tailscale.com/admin/machines) page in the admin console,
selecting the relevant device, then finding the ID in the Machine Details section.
You can also [list all devices in the tailnet](#list-tailnet-devices) to get their `nodeId` values.
(A device's numeric `id` value can also be used in API calls, but `nodeId` is preferred.)
### Attributes
``` jsonc
{
// addresses (array of strings) is a list of Tailscale IP
// addresses for the device, including both IPv4 (formatted as 100.x.y.z)
// and IPv6 (formatted as fd7a:115c:a1e0:a:b:c:d:e) addresses.
"addresses": [
"100.87.74.78",
"fd7a:115c:a1e0:ac82:4843:ca90:697d:c36e"
],
// id (string) is the legacy identifier for a device; you
// can supply this value wherever {deviceId} is indicated in the
// endpoint. Note that although "id" is still accepted, "nodeId" is
// preferred.
"id": "393735751060",
// nodeID (string) is the preferred identifier for a device;
// supply this value wherever {deviceId} is indicated in the endpoint.
"nodeId": "n5SUKe8CNTRL",
// user (string) is the user who registered the node. For untagged nodes,
// this user is the device owner.
"user": "amelie@example.com",
// name (string) is the MagicDNS name of the device.
// Learn more about MagicDNS at https://tailscale.com/kb/1081/.
"name": "pangolin.tailfe8c.ts.net",
// hostname (string) is the machine name in the admin console
// Learn more about machine names at https://tailscale.com/kb/1098/.
"hostname": "pangolin",
// clientVersion (string) is the version of the Tailscale client
// software; this is empty for external devices.
"clientVersion": "",
// updateAvailable (boolean) is 'true' if a Tailscale client version
// upgrade is available. This value is empty for external devices.
"updateAvailable": false,
// os (string) is the operating system that the device is running.
"os": "linux",
// created (string) is the date on which the device was added
// to the tailnet; this is empty for external devices.
"created": "2022-12-01T05:23:30Z",
// lastSeen (string) is when device was last active on the tailnet.
"lastSeen": "2022-12-01T05:23:30Z",
// keyExpiryDisabled (boolean) is 'true' if the keys for the device
// will not expire. Learn more at https://tailscale.com/kb/1028/.
"keyExpiryDisabled": true,
// expires (string) is the expiration date of the device's auth key.
// Learn more about key expiry at https://tailscale.com/kb/1028/.
"expires": "2023-05-30T04:44:05Z",
// authorized (boolean) is 'true' if the device has been
// authorized to join the tailnet; otherwise, 'false'. Learn
// more about device authorization at https://tailscale.com/kb/1099/.
"authorized": true,
// isExternal (boolean) if 'true', indicates that a device is not
// a member of the tailnet, but is shared in to the tailnet;
// if 'false', the device is a member of the tailnet.
// Learn more about node sharing at https://tailscale.com/kb/1084/.
"isExternal": true,
// machineKey (string) is for internal use and is not required for
// any API operations. This value is empty for external devices.
"machineKey": "",
// nodeKey (string) is mostly for internal use, required for select
// operations, such as adding a node to a locked tailnet.
// Learn about tailnet locks at https://tailscale.com/kb/1226/.
"nodeKey": "nodekey:01234567890abcdef",
// blocksIncomingConnections (boolean) is 'true' if the device is not
// allowed to accept any connections over Tailscale, including pings.
// Learn more in the "Allow incoming connections"
// section of https://tailscale.com/kb/1072/.
"blocksIncomingConnections": false,
// enabledRoutes (array of strings) are the subnet routes for this
// device that have been approved by the tailnet admin.
// Learn more about subnet routes at https://tailscale.com/kb/1019/.
"enabledRoutes" : [
"10.0.0.0/16",
"192.168.1.0/24",
],
// advertisedRoutes (array of strings) are the subnets this device
// intends to expose.
// Learn more about subnet routes at https://tailscale.com/kb/1019/.
"advertisedRoutes" : [
"10.0.0.0/16",
"192.168.1.0/24",
],
// clientConnectivity provides a report on the device's current physical
// network conditions.
"clientConnectivity": {
// endpoints (array of strings) Client's magicsock UDP IP:port
// endpoints (IPv4 or IPv6)
"endpoints":[
"199.9.14.201:59128",
"192.68.0.21:59128"
],
// derp (string) is the IP:port of the DERP server currently being used.
// Learn about DERP servers at https://tailscale.com/kb/1232/.
"derp":"",
// mappingVariesByDestIP (boolean) is 'true' if the host's NAT mappings
// vary based on the destination IP.
"mappingVariesByDestIP":false,
// latency (JSON object) lists DERP server locations and their current
// latency; "preferred" is 'true' for the node's preferred DERP
// server for incoming traffic.
"latency":{
"Dallas":{
"latencyMs":60.463043
},
"New York City":{
"preferred":true,
"latencyMs":31.323811
},
},
// clientSupports (JSON object) identifies features supported by the client.
"clientSupports":{
// hairpinning (boolean) is 'true' if your router can route connections
// from endpoints on your LAN back to your LAN using those endpoints’
// globally-mapped IPv4 addresses/ports
"hairPinning":false,
// ipv6 (boolean) is 'true' if the device OS supports IPv6,
// regardless of whether IPv6 internet connectivity is available.
"ipv6":false,
// pcp (boolean) is 'true' if PCP port-mapping service exists on
// your router.
"pcp":false,
// pmp (boolean) is 'true' if NAT-PMP port-mapping service exists
// on your router.
"pmp":false,
// udp (boolean) is 'true' if UDP traffic is enabled on the
// current network; if 'false', Tailscale may be unable to make
// direct connections, and will rely on our DERP servers.
"udp":true,
// upnp (boolean) is 'true' if UPnP port-mapping service exists
// on your router.
"upnp":false
},
},
// tags (array of strings) let you assign an identity to a device that
// is separate from human users, and use it as part of an ACL to restrict
// access. Once a device is tagged, the tag is the owner of that device.
// A single node can have multiple tags assigned. This value is empty for
// external devices.
// Learn more about tags at https://tailscale.com/kb/1068/.
"tags": [
"tag:golink"
],
// tailnetLockError (string) indicates an issue with the tailnet lock
// node-key signature on this device.
// This field is only populated when tailnet lock is enabled.
"tailnetLockError": "",
// tailnetLockKey (string) is the node's tailnet lock key. Every node
// generates a tailnet lock key (so the value will be present) even if
// tailnet lock is not enabled.
// Learn more about tailnet lock at https://tailscale.com/kb/1226/.
"tailnetLockKey": "",
}
```
### Subnet routes
Devices within a tailnet can be set up as subnet routers.
A subnet router acts as a gateway, relaying traffic from your Tailscale network onto your physical subnet.
Setting up subnet routers exposes routes to other devices in the tailnet.
Learn more about [subnet routers](https://tailscale.com/kb/1019).
A device can act as a subnet router if its subnet routes are both advertised and enabled.
This is a two-step process, but the steps can occur in any order:
- The device that intends to act as a subnet router exposes its routes by **advertising** them.
This is done in the Tailscale command-line interface.
- The tailnet admin must approve the routes by **enabling** them.
This is done in the [**Machines**](https://login.tailscale.com/admin/machines) page of the Tailscale admin console
or [via the API](#set-device-routes).
If a device has advertised routes, they are not exposed to traffic until they are enabled by the tailnet admin.
Conversely, if a tailnet admin pre-approves certain routes by enabling them, they are not available for routing until the device in question has advertised them.
The API exposes two methods for dealing with subnet routes:
- Get routes: [`GET /api/v2/device/{deviceID}/routes`](#get-device-routes) to fetch lists of advertised and enabled routes for a device
- Set routes: [`POST /api/v2/device/{deviceID}/routes`](#set-device-routes) to set enabled routes for a device
<a name="device-get"></a>
## Get device
``` http
GET /api/v2/device/{deviceid}
```
Retrieve the details for the specified device.
This returns a JSON `device` object listing device attributes.
### Parameters
#### `deviceid` (required in URL path)
The ID of the device.
#### `fields` (optional in query string)
Controls whether the response returns **all** object fields or only a predefined subset of fields.
Currently, there are two supported options:
- **`all`:** return all object fields in the response
- **`default`:** return all object fields **except**:
- `enabledRoutes`
- `advertisedRoutes`
- `clientConnectivity` (which contains the following fields: `mappingVariesByDestIP`, `derp`, `endpoints`, `latency`, and `clientSupports`)
### Request example
``` sh
curl "https://api.tailscale.com/api/v2/device/12345?fields=all" \
-u "tskey-api-xxxxx:"
```
### Response
``` jsonc
{
"addresses":[
"100.71.74.78",
"fd7a:115c:a1e0:ac82:4843:ca90:697d:c36e"
],
"id":"12345",
// Additional fields as documented in device "Attributes" section above
}
{
"addresses":[
"100.74.66.78",
"fd7a:115c:a1e0:ac82:4843:ca90:697d:c36f"
],
"id":"67890",
// Additional fields as documented in device "Attributes" section above
}
```
<a href="device-delete"></a>
## Delete device
``` http
DELETE /api/v2/device/{deviceID}
```
Deletes the supplied device from its tailnet.
The device must belong to the user's tailnet.
Deleting shared/external devices is not supported.
### Parameters
#### `deviceid` (required in URL path)
The ID of the device.
### Request example
``` sh
curl -X DELETE 'https://api.tailscale.com/api/v2/device/12345' \
-u "tskey-api-xxxxx:"
```
### Response
If successful, the response should be empty:
``` http
HTTP/1.1 200 OK
```
If the device is not owned by your tailnet:
``` http
HTTP/1.1 501 Not Implemented
...
{"message":"cannot delete devices outside of your tailnet"}
```
<a href="device-routes-get">
## Get device routes
3 years ago
``` http
GET /api/v2/device/{deviceID}/routes
```
Retrieve the list of [subnet routes](#subnet-routes) that a device is advertising, as well as those that are enabled for it:
- **Enabled routes:** The subnet routes for this device that have been approved by the tailnet admin.
- **Advertised routes:** The subnets this device intends to expose.
### Parameters
#### `deviceid` (required in URL path)
The ID of the device.
### Request example
``` sh
curl "https://api.tailscale.com/api/v2/device/11055/routes" \
-u "tskey-api-xxxxx:"
```
### Response
Returns the enabled and advertised subnet routes for a device.
``` jsonc
{
"advertisedRoutes" : [
"10.0.0.0/16",
"192.168.1.0/24"
],
"enabledRoutes" : []
}
```
<a href="device-routes-post"></a>
3 years ago
## Set device routes
``` http
POST /api/v2/device/{deviceID}/routes
```
Sets a device's enabled [subnet routes](#subnet-routes) by replacing the existing list of subnet routes with the supplied parameters.
Advertised routes cannot be set through the API, since they must be set directly on the device.
### Parameters
#### `deviceid` (required in URL path)
The ID of the device.
#### `routes` (required in `POST` body)
The new list of enabled subnet routes.
``` jsonc
{
"routes": ["10.0.0.0/16", "192.168.1.0/24"]
}
```
### Request example
``` sh
curl "https://api.tailscale.com/api/v2/device/11055/routes" \
-u "tskey-api-xxxxx:" \
--data-binary '{"routes": ["10.0.0.0/16", "192.168.1.0/24"]}'
```
### Response
Returns the enabled and advertised subnet routes for a device.
``` jsonc
{
"advertisedRoutes" : [
"10.0.0.0/16",
"192.168.1.0/24"
],
"enabledRoutes" : [
"10.0.0.0/16",
"192.168.1.0/24"
]
}
```
<a href="#device-authorized-post"></a>
## Authorize device
``` http
POST /api/v2/device/{deviceID}/authorized
```
Authorize a device.
This call marks a device as authorized or revokes its authorization for tailnets where device authorization is required, according to the `authorized` field in the payload.
This returns a successful 2xx response with an empty JSON object in the response body.
### Parameters
#### `deviceid` (required in URL path)
The ID of the device.
#### `authorized` (required in `POST` body)
Specify whether the device is authorized. False to deauthorize an authorized device, and true to authorize a new device or to re-authorize a previously deauthorized device.
``` jsonc
{
"authorized": true
}
```
### Request example
``` sh
curl "https://api.tailscale.com/api/v2/device/11055/authorized" \
-u "tskey-api-xxxxx:" \
--data-binary '{"authorized": true}'
```
### Response
The response is 2xx on success. The response body is currently an empty JSON object.
<a href="device-tags-post"></a>
## Update device tags
``` http
POST /api/v2/device/{deviceID}/tags
```
Update the tags set on a device.
Tags let you assign an identity to a device that is separate from human users, and use that identity as part of an ACL to restrict access.
Tags are similar to role accounts, but more flexible.
Tags are created in the tailnet policy file by defining the tag and an owner of the tag.
Once a device is tagged, the tag is the owner of that device.
A single node can have multiple tags assigned.
Consult the policy file for your tailnet in the [admin console](https://login.tailscale.com/admin/acls) for the list of tags that have been created for your tailnet.
Learn more about [tags](https://tailscale.com/kb/1068/).
This returns a 2xx code if successful, with an empty JSON object in the response body.
### Parameters
#### `deviceid` (required in URL path)
The ID of the device.
#### `tags` (required in `POST` body)
The new list of tags for the device.
``` jsonc
{
"tags": ["tag:foo", "tag:bar"]
}
```
### Request example
``` sh
curl "https://api.tailscale.com/api/v2/device/11055/tags" \
-u "tskey-api-xxxxx:" \
--data-binary '{"tags": ["tag:foo", "tag:bar"]}'
```
### Response
The response is 2xx on success. The response body is currently an empty JSON object.
If the tags supplied in the `POST` call do not exist in the tailnet policy file, the response is '400 Bad Request':
``` jsonc
{
"message": "requested tags [tag:madeup tag:wrongexample] are invalid or not permitted"
}
```
<a href="device-key-post"><a>
## Update device key
``` http
POST /api/v2/device/{deviceID}/key
```
Update properties of the device key.
### Parameters
#### `deviceid` (required in URL path)
The ID of the device.
#### `keyExpiryDisabled` (optional in `POST` body)
Disable or enable the expiry of the device's node key.
When a device is added to a tailnet, its key expiry is set according to the tailnet's [key expiry](https://tailscale.com/kb/1028/) setting.
If the key is not refreshed and expires, the device can no longer communicate with other devices in the tailnet.
Set `"keyExpiryDisabled": true` to disable key expiry for the device and allow it to rejoin the tailnet (for example to access an accidentally expired device).
You can then call this method again with `"keyExpiryDisabled": false` to re-enable expiry.
``` jsonc
{
"keyExpiryDisabled": true
}
```
- If `true`, disable the device's key expiry.
The original key expiry time is still maintained.
Upon re-enabling, the key will expire at that original time.
- If `false`, enable the device's key expiry.
Sets the key to expire at the original expiry time prior to disabling.
The key may already have expired. In that case, the device must be re-authenticated.
- Empty value will not change the key expiry.
This returns a 2xx code on success, with an empty JSON object in the response body.
### Request example
``` sh
curl "https://api.tailscale.com/api/v2/device/11055/key" \
-u "tskey-api-xxxxx:" \
--data-binary '{"keyExpiryDisabled": true}'
```
### Response
The response is 2xx on success. The response body is currently an empty JSON object.
# Tailnet
A tailnet is your private network, composed of all the devices on it and their configuration.
Learn more about [tailnets](https://tailscale.com/kb/1136/).
When specifying a tailnet in the API, you can:
3 years ago
- Provide a dash (`-`) to reference the default tailnet of the access token being used to make the API call.
This is the best option for most users.
Your API calls would start:
``` sh
curl "https://api.tailscale.com/api/v2/tailnet/-/..."
```
- Provide the **organization** name found on the **[General Settings](https://login.tailscale.com/admin/settings/general)**
page of the Tailscale admin console (not to be confused with the "tailnet name" found in the DNS tab).
For example, if your organization name is `alice@gmail.com`, your API calls would start:
``` sh
curl "https://api.tailscale.com/api/v2/tailnet/alice@gmail.com/..."
```
## Policy File
The tailnet policy file contains access control lists and related configuration.
The policy file is expressed using "[HuJSON](https://github.com/tailscale/hujson#readme)"
(human JSON, a superset of JSON that allows comments and trailing commas).
Most policy file API methods can also return regular JSON for compatibility with other tools.
Learn more about [network access controls](https://tailscale.com/kb/1018/).
<a href="tailnet-acl-get"></a>
## Get Policy File
``` http
GET /api/v2/tailnet/{tailnet}/acl
```
Retrieves the current policy file for the given tailnet; this includes the ACL along with the rules and tests that have been defined.
This method can return the policy file as JSON or HuJSON, depending on the `Accept` header.
The response also includes an `ETag` header, which can be optionally included when [updating the policy file](#update-policy-file) to avoid missed updates.
### Parameters
3 years ago
#### `tailnet` (required in URL path)
The tailnet organization name.
#### `Accept` (optional in request header)
Response is encoded as JSON if `application/json` is requested, otherwise HuJSON will be returned.
#### `details` (optional in query string)
Request a detailed description of the tailnet policy file by providing `details=1` in the URL query string.
If using this, do not supply an `Accept` parameter in the header.
The response will contain a JSON object with the fields:
- **tailnet policy file:** a base64-encoded string representation of the huJSON format
- **warnings:** array of strings for syntactically valid but nonsensical entries
- **errors:** an array of strings for parsing failures
### Request example (response in HuJSON format)
``` sh
curl "https://api.tailscale.com/api/v2/tailnet/example.com/acl" \
-u "tskey-api-xxxxx:" \
```
### Response in HuJSON format
On success, returns a 200 status code and the tailnet policy file in HuJSON format.
No errors or warnings are returned.
``` jsonc
...
Content-Type: application/hujson
Etag: "e0b2816b418b3f266309d94426ac7668ab3c1fa87798785bf82f1085cc2f6d9c"
...
// Example/default ACLs for unrestricted connections.
{
// Declare static groups of users beyond those in the identity service.
"groups": {
"group:example": ["user1@example.com", "user2@example.com"],
},
// Declare convenient hostname aliases to use in place of IP addresses.
"hosts": {
"example-host-1": "100.100.100.100",
},
// Access control lists.
"acls": [
// Match absolutely everything.
// Comment this section out if you want to define specific restrictions.
{"action": "accept", "src": ["*"], "dst": ["*:*"]},
],
}
```