SDKs
Nodejs

NodeJS SDK

This SDK is aimed at users who code in NodeJS, auto-signing and simplifying the calling process. Download the following file and save it as api.js:

Functions

.call(params)

Calls the API. Return value: Promise.

ParameterTypeDefaultDescription
paramsObjectNoneCalling parameters, consult the API documentation for more information. The parameters PublicKey, PrivateKey and Signature are not required.

.localImg2Base64(path)

Get an image or mask from a local disk and encode it in Base64 format. Return value: Promise

ParameterTypeDefaultDescription
pathStringNoneThe file path of an image or mask.

.remoteImg2Base64(url)

Get an image or mask remotely and encode it in Base64 format. Return value: Promise

ParameterTypeDefaultDescription
urlStringNoneThe URL address of an image or mask. If the link does not start with "https://", the SDK will try to automatically fix it.

.pollingTask(taskUID, interval, callback)

Poll a task. Return value: Promise.

ParameterTypeDefaultDescription
taskUIDStringNoneThe task ID was obtained after calling the Generate and Modify class APIs.
intervalInt3000Optional. Polling interval in milliseconds.
callbackFunctionnullOptional. A callback function that returns an Object containing detailed information about the current task.
If the interval isn't set, the callback can be used directly as the second argument.

.setKeys(publicKey, privateKey)

Set the public and private keys required to call the API. Return value: None

ParameterTypeDefaultDescription
publicKeyStringNonePublic key, available on the User Center page.
privateKeyStringNonePrivate key, available on the User Center page.
gatewayStringhttps://api.picpik.ai/ (opens in a new tab)Optional. The API gateway of the global site. If your account belongs to the site of Chinese mainland, the gateway should be https://api.picpikai.com/.
If the link does not start with "https://", the SDK will try to fix it automatically.

Demo

A demo of generating a bird and remove the background.

const api = require("./api");
 
// Set your API Keys. Keys can be found on the user account page after logging into the official website.
api.setKeys(
    publicKey = "Your Public Key",
    privateKey = "Your Private Key"
);
 
// Get the models' list. A main model and relevant parameters are required when generating images. Different from calling the APIs directly, the SDK will automatically provide common 
api.call({
    Action: "ListModels"
})
.then(data => {
// Generate an image by specifying a model from the models' list.
    let model = data.MainModels["PICPIK General"];
    return api.call({
        Action: "GenerateImages",
        MainModel: model,
        PositivePrompts: ["One Bird"],
    })
})
.then(data => {
// Polling the task by it's task UID.
    let taskUID = data.TaskUID;
    return api.pollingTask(taskUID, details => {
        console.log(details.Status);
    });
})
.then(data => {
// Get the URL of the generated image and encode it in Base64.
    let imageURL = data.GeneratedImageList[0];
    return api.remoteImg2Base64(imageURL);
})
.then(base64Img => {
// Remove the background
    return api.call({
        Action: "RemoveBackground",
        RawImage: base64Img
    });
})
.then(data => {
// Polling the task by it's task UID.
    let taskUID = data.TaskUID;
    return api.pollingTask(taskUID, details => {
        console.log(details.Status);
    });
})
.then(data => {
// Get the URL of the generated image after finishing the task.
    let imageURL = "https://" + data.GeneratedImageList[0];
    console.log(imageURL);
})
.catch(error => {
// Catch errors may happen during the process.
    console.error(JSON.stringify(error));
});