API usage examples
Portainer exposes an HTTP API that you can use to automate everything you do via the Portainer UI. You can also use Portainer as a gateway (HTTP queries against the Portainer API) to the underlying Docker/Kubernetes API.
The following examples use httpie to execute API calls against Portainer.

Initialize the admin password

On a fresh install of Portainer, you need to create an admin account to initialize Portainer. You will be asked for this when you visit the Portainer URL for the first time. You can achieve the same outcome using this API call:
1
http POST <portainer url>/api/users/admin/init Username="<admin username>" Password="<adminpassword>"
Copied!

Authenticate against the API using the admin account

1
http POST <portainer url>/api/auth Username="<admin username>" Password="<adminpassword>"
Copied!
The response is a JSON object containing the JWT token inside the jwt field. You will need to pass this token inside the authorization header when executing an authentication query against the API.
1
{
2
"jwt":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwidXNlcm5hbWUiOiJhZG1pbiIsInJvbGUiOjEsImV4cCI6MTQ5OTM3NjE1NH0.NJ6vE8FY1WG6jsRQzfMqeatJ4vh2TWAeeYfDhP71YEE"
3
}
Copied!
The value of the authorization header must be of the form Bearer <JWT_TOKEN>:
1
Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwidXNlcm5hbWUiOiJhZG1pbiIsInJvbGUiOjEsImV4cCI6MTQ5OTM3NjE1NH0.NJ6vE8FY1WG6jsRQzfMqeatJ4vh2TWAeeYfDhP71YEE
Copied!
This token is valid for 8 hours. Once it expires, you will need to generate another token to execute authenticated queries.

Adding a new environment

On a fresh install, Portainer has no environments configured. You will first need to add an environment for Portainer to manage.
You can add an environment to manage via the Portainer API, or via the web interface both during the initial setup and after setup is complete.

Execute Docker queries against a specific environment

The Portainer HTTP API endpoint acts as a reverse-proxy to the Docker HTTP API and can be used to execute any of the Docker HTTP API requests:
/api/endpoints/<ENVIRONMENT_ID>/docker
Read Docker's API documentation to learn how to query the Docker Engine.

List all containers

This call lists all of the containers available in a specific environment:
1
http GET <portainer url>/api/endpoints/1/docker/containers/json \
2
"Authorization: Bearer <jwt token>" \
3
all==true
Copied!
The response is identical to that returned by the ContainerList operation of the Docker API. See Docker's documentation about this operation.

Create a container

You can create a container in a specific environment using the Portainer HTTP API as a gateway. The following query will create a new Docker container inside the environment using ID 1. The container will be named web01 and will use the nginx:latest Docker image. It will publish container port 80 on port 8080 on the host.
1
http POST <portainer url>/api/endpoints/1/docker/containers/create \
2
"Authorization: Bearer <jwt token>" \
3
name=="web01" Image="nginx:latest" \
4
ExposedPorts:='{ "80/tcp": {} }' \
5
HostConfig:='{ "PortBindings": { "80/tcp": [{ "HostPort": "8080" }] } }'
Copied!
The response is identical to that returned by the ContainerCreate operation of the Docker API. See Docker's documentation about this operation.
Here is an example response:
1
{
2
"Id": "5fc2a93d7a3d426a1c3937436697fc5e5343cc375226f6110283200bede3b107",
3
"Warnings": null
4
}
Copied!
You will need the container ID in order to execute actions against that container.

Start a container

Using the ID you retrieved previously, you can start your new container using this endpoint:
/api/endpoints/<ENVIRONMENT_ID>/docker/containers/<CONTAINER_ID>/start
1
http POST <portainer url>/api/endpoints/1/docker/containers/5fc2a93d7a3d426a1c3937436697fc5e5343cc375226f6110283200bede3b107/start \
2
"Authorization: Bearer <jwt token>"
Copied!
The response is identical to that returned by the ContainerStart operation of the Docker API. See Docker's documentation about this operation.

Delete a container

You can create a container using the endpoint /api/endpoints/<ENVIRONMENT_ID>/docker/containers/:
1
http DELETE <portainer url>/api/endpoints/1/docker/containers/5fc2a93d7a3d426a1c3937436697fc5e5343cc375226f6110283200bede3b107 \
2
"Authorization: <jwt token>" \
3
force==true
Copied!
The response is identical to that returned by the ContainerDelete operation of the Docker API. See Docker's documentation about this operation.
Last modified 18d ago