Accessing the Portainer API
To access the Portainer API, you will need a few things:
  • A user in Portainer
  • An access token for that user
  • The ability to make HTTPS requests to the Portainer server on port 9443 (or 9000 for legacy HTTP), or

Creating a new user

API access is provided on a per-user basis, with each users' API access dependent on that user's permissions within Portainer. For example, if your user had access to only one environment, API calls for that user would also be restricted to that environment.
To create a new user within Portainer, refer to our documentation:
Once the user has been created, log in to Portainer as that user to create an API access token.

Creating an access token

Once the user has been created, you can add an access token to that user. The access token will provide the same level of access to Portainer functionality as would be available to that user had they logged into the Portainer UI.
Once logged in as the user, click on my account in the top right.
Scroll down to the Access tokens section. Here you can see any access tokens that exist for the user.
To add a new access token, click the Add access token button. You will be taken to a new page where you can set a Description for your access token. We recommend making this something recognizable for future reference.
Once you have provided a description, click the Add access token button to generate your access token.
Your new access token will now be displayed. Please copy the access token and keep it in a safe place, as you will not be able to view the token again after creation.
When you have copied the access token, click the Done button to return to the User settings page. Your access token is ready to use.

Using your access token

Now that you have created a user and access token, you are ready to access the API. The Portainer API follows the RESTful architecture, accepting GET / POST / PUT / DELETE requests and responding with JSON objects.
The following examples use httpie to execute API calls against Portainer. Feel free to replace this with your method of choice.
To make an API request, you will need to include your access token in the X-API-Key header to authenticate your request. For example, you can use the /stacks endpoint to list the stacks you have access to:
1
http GET https://portainer-url:9443/api/stacks X-API-Key:your_api_key_here
Copied!
This will return a JSON object listing your stacks:
1
[
2
{
3
"AdditionalFiles": null,
4
"AutoUpdate": null,
5
"CreatedBy": "admin",
6
"CreationDate": 1631852794,
7
"EndpointId": 4,
8
"EntryPoint": "docker-compose.yml",
9
"Env": null,
10
"GitConfig": {
11
"Authentication": null,
12
"ConfigFilePath": "docker-compose.yml",
13
"ConfigHash": "2e71920bf1ee1bbac976d320f8f274411fba3bad",
14
"ReferenceName": "refs/heads/master",
15
"URL": "https://github.com/mygithubaccount/wordpress-stack"
16
},
17
"Id": 5,
18
"IsComposeFormat": true,
19
"Name": "",
20
"Namespace": "my-namespace",
21
"ProjectPath": "/data/compose/5",
22
"ResourceControl": null,
23
"Status": 1,
24
"SwarmId": "",
25
"Type": 3,
26
"UpdateDate": 0,
27
"UpdatedBy": ""
28
},
29
]
Copied!
If a user tries to access an area they do not have permission to access, an error message will be returned. For example, assume that a non-administrator user attempted to access the /settings endpoint, which requires administrator access:
1
http GET https://portainer:9443/api/settings X-API-Key:your_api_key_here
Copied!
The user would be presented with the following response:
1
{
2
"details": "Unauthorized",
3
"message": "Access denied"
4
}
Copied!
Now that you have access to the Portainer API, you can learn more about how to use it from the API documentation and our usage examples.
Last modified 1mo ago