Skip to main content

Usage

The following code snippets shows how to use the Solidgrids API to upload an image, process it, and get the URL of the processed image.

Setting up the environment

First, you need to set up your environment by importing the required libraries and setting the SG_API_TOKEN to your access token.

import os
import pathlib
import requests

SG_API_TOKEN = "{{accessToken}}"

Uploading the Image

Next, you need to read the image file, extract its filename and content type, and make a POST request to the /api/upload endpoint to get a signed URL to upload the image to S3.

# Read the file and add it to the request
file_path = "/path/to/image.png"
file = open(file_path, "rb")
file_size = os.stat(file_path).st_size

# Extract the filename from the full path
name = pathlib.Path(file_path).name

# Extract the content type from the filename
content_type = pathlib.Path(file_path).suffix[1:]

# 1. Make a POST request to the /api/upload endpoint to get a signed S3 URL
headers = {
"Content-Type": "application/json",
"Authorization": SG_API_TOKEN,
}
data = {
"contentType": content_type,
"name": name,
}
res = requests.post("https://app.solidgrids.com/api/upload", headers=headers, json=data)
data = res.json()
signed_url = data["signedRequest"]
s3_key = data["key"]

# 2. Upload the file to S3 using the signed URL
headers = {
"Content-Type": f"image/{content_type}",
"Content-Length": str(file_size),
"Access-Control-Allow-Origin": "\*",
"Access-Control-Allow-Methods": "PUT",
"Access-Control-Allow-Headers": "X-Requested-With, content-type, Authorization",
"x-amz-acl": "public-read",
}
requests.put(signed_url, headers=headers, data=file)

Processing the Image

Finally, make a POST request to the /api/pipe endpoint to get a signed URL for the processed image.

headers = {
"Content-Type": "application/json",
"Authorization": SG_API_TOKEN,
}
data = {
"key": s3_key,
}
processed_res = requests.post(
"https://app.solidgrids.com/api/pipe", headers=headers, json=data
)
processed_url = processed_res.json()["signedRequest"]

print(f"Processed image is available at {processed_url}")

With these steps, you can easily upload and process an image using the Solidgrids API.