How to retry requests automatically
In scenarios where API requests might fail, implementing a retry mechanism can ensure successful execution. This guide demonstrates how to adjust image properties using a retry strategy with the Picsart TypeScript SDK. By setting retries and delays, you can manage transient errors without disrupting your workflow. Be mindful of additional credit charges, and avoid setting retry values greater than 1 to minimize costs. Learn how to enhance the reliability of your API calls with our step-by-step example.
Before you start
Check these out before you start
Handle retries gracefully
In scenarios where API requests may fail, implementing a retry mechanism can help ensure successful execution. Here’s how to adjust image properties with a retry strategy:
import PicsartEnterprise, { AdjustRequest, ImageApi } from "../src";
(async () => {
const imageApi = PicsartEnterprise.createImageApi(process.env.PICSART_API_KEY!);
const imageSource = ImageApi.fromUrl(process.env.IMAGE_URL!);
const result = await imageApi.adjust(
new AdjustRequest()
.setImage(imageSource)
.setBrightness(40) // Adjusts the brightness of the image
.setRetries(3) // Number of retry attempts in case of failure
.setRetryDelay(5000) // Delay in milliseconds between retry attempts
);
console.log(result.image.url); // The URL of the adjusted image
})();
In this example, the brightness of the image is increased by 40
, and if the API request fails, it will automatically retry up to 3
times with a 5-second
delay between attempts. This ensures that transient errors do not hinder your image processing workflow.
Retries May Result in Additional Credit Charges
Please be aware that every API call incurs a credit charge. Therefore, enabling retries can lead to additional charges. We recommend carefully considering the use of retries and configuring them judiciously to avoid unnecessary costs. Additionally, we do not recommend setting retry values greater than 1 to minimize the risk of incurring excessive charges.
Updated 2 months ago