Quickstart¶
This guide details the steps needed to install or update the Picsart Creative APIs Python SDK (referred further as Picsart Python SDK)
Installation¶
To use Picsart Python SDK, you first need to install it and its dependencies.
Install or update Python¶
Before installing Picsart Python SDK, install Python 3.9 or later.
For information about how to get the latest version of Python, see the official Python documentation.
Install Picsart Python SDK¶
Install the latest picsart-sdk release via pip:
pip install git+https://github.com/PicsArt/picsart-creative-apis-python-sdk
If your project requires a specific version of picsart-sdk, or has compatibility concerns with certain versions, you may provide constraints when installing:
# Install picsart-sdk version 1.0 specifically
pip install git+https://github.com/PicsArt/picsart-creative-apis-python-sdk@1.0.0
Note
The latest development version of picsart-sdk is on GitHub.
Configuration¶
In order to use the Picsart Python SDK, you need to obtain an API KEY from picsart.io portal.
The framework will automatically look for the API KEY in the PICSART_API_KEY
environment variable.
If the environment variable isn’t set, you will have to pass it programmatically. Refer the section below.
Using Picsart Python SDK¶
Create a client¶
Option 1: Using environment variable for the API Key
If the PICSART_API_KEY
environment variable is set, you can quickly create a client:
import picsart_sdk
from picsart_sdk import PicsartAPI
from picsart_sdk.clients import RemoveBackgroundClient
client: RemoveBackgroundClient = picsart_sdk.client(PicsartAPI.REMOVE_BACKGROUND)
Option 2: Passing the API Key manually
You can also pass the API key directly to the API client:
import picsart_sdk
from picsart_sdk import PicsartAPI
from picsart_sdk.clients import RemoveBackgroundClient
client: RemoveBackgroundClient = picsart_sdk.client(
PicsartAPI.REMOVE_BACKGROUND,
api_key="YOUR_API_KEY"
)
Note
We recommend always using Python type hinting, such as upload_client: UploadClient = picsart_sdk.client(...)
, to fully leverage IDE autocompletion and improve code readability.
Remove the background of an image
After acquiring the client, you can call specific methods of that client. In the case of RemoveBackgroundClient
,
you can call remove_background
method.
from picsart_sdk.api_responses import ApiResponse
...
response: ApiResponse = client.remove_background(image_path="./file.jpg")
print(response.data.url)
The image without background will be available in the Picsart CDN at the URL from response.data.url
.
Note
You can find an extensive list of code snippets in the examples
folder from the GitHub repo.
Create an async client¶
PicsartAPI supports the creation of async clients to perform non-blocking HTTP calls to the Picsart API. Async clients leverage Python’s asyncio capabilities, enabling efficient handling of multiple requests concurrently.
How to Create an Async Client
To create an async client for any PicsartAPI service, follow these steps:
Import the appropriate client class for the desired PicsartAPI service. Instantiate the client using the async factory method or constructor, depending on the implementation.
You can find a detailed example of creating and using an async client in the async_client.py file located in the examples folder.
Debugging¶
You can enable extra logging providing the following environment variables:
PICSART_SDK_LOGGING_LEVEL
: Controls the logging level. Possible values:CRITICAL
,ERROR
,WARNING
,INFO
,DEBUG
,NOTSET
. IfPICSART_SDK_LOGGING_LEVEL
is not provided or contains an invalid value, logging will be disabled.PICSART_SDK_LOG_HTTP_CALLS
: Enables logging of HTTP calls made to the Picsart API. Possible values:true
orfalse
.PICSART_SDK_LOG_HTTP_CALLS_HEADERS
: Logs the HTTP headers used in API calls. Possible values:true
orfalse
. Note: Enabling this will log sensitive information, including thePICSART_API_KEY
.
Other environment variables¶
PICSART_SDK_DEFAULT_HTTP_TIMEOUT_SECONDS
: Control the HTTP timeout in seconds for the API calls. These value is only for the client. If the timeout is happening in the backend infrastructure you can still get a timeout error.