npm.io
0.2.0 • Published 1 year ago

portainer-api-client

Licence
MIT
Version
0.2.0
Deps
1
Size
8 kB
Vulns
0
Weekly
0
Stars
2

Portainer API Client

Make requests against a Portainer API, authenticating with username/password or key

Example: Get a container's status with username/password auth

import PortainerClient from 'portainer-api-client';

const portainerURL = process.env.PORTAINER_URL;
const portainerUsername = process.env.PORTAINER_USERNAME;
const portainerPassword = process.env.PORTAINER_PASSWORD;

const portainer = new PortainerClient({
    url: portainerURL,
    username: portainerUsername,
    password: portainerPassword
});

const container = await portainer.callAPIWithKey('GET', '/api/endpoints/1/docker/containers/my_container_name/json');
console.log(`Container's status is ${container.State?.Status}`);

Example: Get a container's status with key auth

import PortainerClient from 'portainer-api-client';

const portainerURL = process.env.PORTAINER_URL;
const portainerAPIKey = process.env.PORTAINER_API_KEY;
const portainerPassword = process.env.PORTAINER_PASSWORD;

const portainer = new PortainerClient({
    url: portainerURL,
    key: portainerAPIKey
});

const container = await portainer.callAPIWithKey('GET', '/api/endpoints/1/docker/containers/my_container_name/json');
console.log(`Container's status is ${container.State?.Status}`);

Example: Update a registry's password with username/password auth

import PortainerClient from 'portainer-api-client';

const portainerURL = process.env.PORTAINER_URL;
const portainerUsername = process.env.PORTAINER_USERNAME;
const portainerPassword = process.env.PORTAINER_PASSWORD;

const portainer = new PortainerClient({
    url: portainerURL,
    username: portainerUsername,
    password: portainerPassword
});

let registries = await portainer.callAPIWithKey('get', '/api/registries');
registry = registries[0]

registry.Username = 'newusername';
registry.Password = 'newusername';

await portainer.callAPIWithKey('PUT', `/api/registries/${registry.Id}`, registry);

TODOs

  • Convenience functions
  • Documentation