Add an endpoint via the Portainer API
Portainer's API lets you perform the same actions as via the Portainer UI, including adding new endpoints. This article explains how to add the following types of endpoints via the API:
    A local endpoint using Docker socket communication.
    A remote endpoint using TCP communication.
    A remote endpoint using TCP communication secured via TLS.
The examples in this article use httpie to make HTTP calls from the command line to the Portainer API. Feel free to replace httpie with your preferred method.

Preparation

After deploying Portainer, you'll need to initialize your admin user. First, initialize the admin password:
1
http POST :9000/api/users/admin/init Username="admin" Password="adminpassword"
Copied!
Now, use the admin account to authenticate against the API. Generate an authorization token for your username. This token will provide you with the same permissions as the user who generated it.
1
http POST :9000/api/auth Username="admin" Password="adminpassword"
Copied!
The response is a JSON object containing the JWT token inside the jwt field. Make a note of this token. You'll use it in the authorization header when making API calls.
1
{
2
"jwt":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwidXNlcm5hbWUiOiJhZG1pbiIsInJvbGUiOjEsImV4cCI6MTQ5OTM3NjE1NH0.NJ6vE8FY1WG6jsRQzfMqeatJ4vh2TWAeeYfDhP71YEE"
3
}
Copied!
The authorization header value must take the form Bearer JWT_TOKEN. Using the above token as an example, the value would look like this:
1
Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwidXNlcm5hbWUiOiJhZG1pbiIsInJvbGUiOjEsImV4cCI6MTQ5OTM3NjE1NH0.NJ6vE8FY1WG6jsRQzfMqeatJ4vh2TWAeeYfDhP71YEE
Copied!
The JWT token is valid for 8 hours after it is generated. Once it expires, you will need to generate a new token.

Adding an endpoint

Adding a local endpoint via the Docker socket

This query will create an endpoint called test-local and will use the Docker socket to communicate with your environment.
This example requires you to bind-mount the Docker socket when running Portainer.
Run the following command:
1
http --form POST :9000/api/endpoints \
2
"Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwidXNlcm5hbWUiOiJhZG1pbiIsInJvbGUiOjEsImV4cCI6MTQ5OTM3NjE1NH0.NJ6vE8FY1WG6jsRQzfMqeatJ4vh2TWAeeYfDhP71YEE" \
3
Name="test-local" EndpointCreationType=1
Copied!
The response is a JSON object representing the endpoint:
1
{
2
"AuthorizedTeams": [],
3
"AuthorizedUsers": [],
4
"Extensions": [],
5
"GroupId": 1,
6
"Id": 1,
7
"Name": "test-local",
8
"PublicURL": "",
9
"Type": 1,
10
"TLSConfig": {
11
"TLS": false,
12
"TLSSkipVerify": false
13
},
14
"Type": 1,
15
"URL": "unix:///var/run/docker.sock"
16
}
Copied!
Make a note of the Id value. It will be used to execute queries against the Docker Engine for the endpoint.

Adding a remote endpoint

This query will create an endpoint called test-remote. It will communicate with your environment over TCP using the IP address 10.0.7.10 and port 2375. Make sure you replace the example values with your own IP address and port.
The Docker API must be exposed on the provided IP address and port. To learn how to do this, refer to the Docker documentation.
Run the following command:
1
http --form POST :9000/api/endpoints \
2
"Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwidXNlcm5hbWUiOiJhZG1pbiIsInJvbGUiOjEsImV4cCI6MTQ5OTM3NjE1NH0.NJ6vE8FY1WG6jsRQzfMqeatJ4vh2TWAeeYfDhP71YEE" \
3
Name="test-remote" URL="tcp://10.0.7.10:2375" EndpointCreationType=1
Copied!
The response is a JSON object representing the endpoint:
1
{
2
"AuthorizedTeams": [],
3
"AuthorizedUsers": [],
4
"Extensions": [],
5
"GroupId": 1,
6
"Id": 1,
7
"Type": 1,
8
"Name": "test-remote",
9
"PublicURL": "",
10
"TLSConfig": {
11
"TLS": false,
12
"TLSSkipVerify": false
13
},
14
"Type": 1,
15
"URL": "tcp://10.0.7.10:2375"
16
}
Copied!
Take a note of the Id value. It will be used to execute queries against the Docker Engine for the endpoint.

Adding a remote endpoint with TLS

This query will create an endpoint called test-remote-tls. It will communicate with your environment over TCP (secured with TLS) using the IP address 10.0.7.10 and port 2376. Make sure you replace the example values with your own IP address and port.
The Docker API must be exposed on the provided IP address and port. To learn how to do this, refer to the Docker documentation.
Run the following command:
1
http --form POST :9000/api/endpoints \
2
"Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwidXNlcm5hbWUiOiJhZG1pbiIsInJvbGUiOjEsImV4cCI6MTQ5OTM3NjE1NH0.NJ6vE8FY1WG6jsRQzfMqeatJ4vh2TWAeeYfDhP71YEE" \
3
Name="test-remote-tls" URL="tcp://10.0.7.10:2376" EndpointCreationType=1 TLS="true" [email protected]/path/to/ca.pem [email protected]/path/to/cert.pem [email protected]/path/to/key.pem
Copied!
The response is a JSON object representing the endpoint:
1
{
2
"AuthorizedTeams": [],
3
"AuthorizedUsers": [],
4
"Extensions": [],
5
"GroupId": 1,
6
"Id": 1,
7
"Type": 1,
8
"Name": "test-remote-tls",
9
"PublicURL": "",
10
"TLSConfig": {
11
"TLS": true,
12
"TLSCACert": "/data/tls/1/ca.pem",
13
"TLSCert": "/data/tls/1/cert.pem",
14
"TLSKey": "/data/tls/1/key.pem",
15
"TLSSkipVerify": false
16
},
17
"Type": 1,
18
"URL": "tcp://10.0.7.10:2376"
19
}
Copied!
Make a note of the Id value. It will be used to execute queries against the Docker Engine for the endpoint.
Last modified 6d ago