Skip to content

Picsart Creative APIs SDK for Typescript. Includes helper methods and functions for Programmable Image APIs (e.g. Remove Background, Upscale, Enhance, Effects) and the GenAI APIs (e.g. Text2Image, Replace, Expand Image).

License

Notifications You must be signed in to change notification settings

PicsArt/picsart-creative-apis-ts-sdk

Repository files navigation

PICSART CREATIVE APIS

Description

This is a TypeScript SDK of Picsart Programmable Image APIs and Picsart GenAI APIs. You can easily do many actions with your images just by adding few lines of code to your JavaScript or Typescript projects.

Installation

To install the package, run the following command:

npm install --save picsart-creative-apis-ts-sdk

Usage

Get an API instance

import  PicsartEnterprise from "picsart-creative-apis-ts-sdk";

// Get an instance of image API
const imageApi = PicsartEnterprise.createImageApi('YOUR_API_KEY');

// Get an instance of GenAI API
const genaiApi = PicsartEnterprise.createGenAIApi('YOUR_API_KEY');

Create an Image instance

// From public image url
const imageSource = ImageApi.fromUrl('https://....');

// From Picsart image id
const imageSource = ImageApi.fromImageId('your-image-id-here');

// From binary
const fileContent = fs.readFileSync("examples/image.png");
const imageSource = ImageApi.fromFile(fileContent);

// From stream
const readStream = fs.createReadStream("examples/image.png");
const imageSource = ImageApi.fromStream(readStream);

Remove background

  const result = await imageApi.removeBackground(
      new RemoveBackgroundRequest()
        .setImage(imageSource)
        .setBgColor("green")
    );

  console.log(result.image.url);
  console.log(result.metadata.creditsAvailable);
  console.log(result.metadata.rateLimitResetTime);
  console.log(result.metadata.rateLimit);
  console.log(result.metadata.rateLimitAvailable);
  console.log(result.metadata.correlationId);

Adjust image

  const result = await imageApi.adjust(
      new AdjustRequest()
        .setImage(imageSource)
        .setBrightness(90)
        .setSharpen(40)
    );
    
  console.log(result.image.url);

Apply effects

  const result = await imageApi.effect(
      new EffectRequest()
        .setImage(imageSource)
        .setEffectName(EffectName.a1972)
    );

  console.log(result.image.url);

Generate background textures

  const result = await imageApi.backgroundTexture(
      new BackgroundTextureRequest()
        .setImage(imageSource)
        .setPattern(TexturePattern.diamond)
        .setHeight(800)
    );

  console.log(result.image.url);

Preview selected effects on an image

  const result = await imageApi.effectPreviews(
      new EffectPreviewsRequest()
        .setImage(imageSource)
        .addEffect(EffectName.a1972)
        .addEffect(EffectName.brnz2)
        .addEffect(EffectName.icy3)
        .addEffect(EffectName.apr3),
    );

  console.log(result.thumbnails);

Enhance face

  const result = await imageApi
  .enhanceFace(
    new EnhanceFaceRequest()
      .setImage(imageSource),
  );

  console.log(result.image.url);

Get balance

  const result = await imageApi.getBalance();
  console.log(result.credits);

Get list of effects

  const result = await imageApi.getEffects();
  console.log(result.effects)

Surface Map

  const result = await imageApi.surfacemap(
      new SurfacemapRequest()
        .setImage(imageSource1)
        .setMask(imageSource2)
        .setSticker(imageSource3)
    );

  console.log(result.image.url);

Text to Image

  const result = await genaiApi.text2Image(
      new Text2ImageRequest()
        .setCount(10)
        .setPrompt("coding all the time")
        .setNegativePrompt("relax and rest")
    );
  console.log(result.images);

Upload Image

  const fileContent = readFileSync("examples/image.png");
  const blob = new Blob([fileContent]);

  const imageSource = ImageApi.fromFile(blob);
  const result = await imageApi.upload(
    new UploadRequest()
      .setImage(imageSource)
  );

  console.log(result.image.url);

Upscale

  const result = await imageApi.upscale(
    new UpscaleRequest()
      .setImage(imageSource)
      .setUpscaleFactor(UpscaleFactor.R2000x2000)
  );

  console.log(result.image.url);

Ultra Enhance

  const result = await imageApi.ultraEnhance(
      new UltraEnhanceRequest()
        .setImage(imageSource)
        .setUpscaleFactor(6),
    );

  console.log(result.image.url);

Ultra Upscale

  const result = await imageApi.ultraUpscale(
      new UltraUpscaleRequest()
        .setImage(imageSource)
        .setUpscaleFactor(4)
        .setMode(ProcessingMode.async),
    );

  console.log(result.image.url);

Build

To build a package use the following command:

npm run build

It will create /lib directory in the root directory of the project which will contain ready to publish npm package.

Adding boilerplate license notice

Run the following command to add the boilerplate license notice in all *.ts files:

npm run license

This command will not only add the header to all new files, but will also fix existing files if there is a need.

To be able to run this on macOS, you need to install GNU sed. If you haven't installed it yet, follow these steps:

  1. Install Homebrew: If you don't have Homebrew installed, open Terminal and run:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  1. Install GNU sed: Once Homebrew is installed, run the following command to install GNU sed:
brew install gnu-sed

Generating API References

API references are generated using TypeDoc based on TSDoc comments in the code. Run the following command to generate it:

npm run generate-docs

This command will generate API documentation in HTML format in /docs directory.

Tests

To run tests, use the following command:

npm test

License

Picsart Creative APIs SDK is provided under the MIT license that can be found in the LICENSE file. By using, distributing, or contributing to this project, you agree to the terms and conditions of this license.

This project has some third-party dependencies, each of which may have independent licensing:

How to contribute?

If you like Picsart Creative APIs SDK and would like to contribute to this open-source project, please check the Contribution guide.