Create image

Generate images from a prompt

You will need an API token to send HTTP requests. See Authentication for instructions.

Quick start

To generate pictures, you will have to call the endpoint once for each prompt you want to use. Each one will generate a new picture.

This will start asynchronous processes that will be handled in the next step.

This is an example of request containing all required fields:

Request
{
  "prompt": "a picture of a cat",
  "aspect_ratio": "16:9"
}

For more information on generation options, see Generation options.


Notification objects returned by this endpoint will inform you of the end of a generation on our servers. We recommend sending a request every 3 seconds at most, to not incur in rate limiting.

Request
{
  "name_list": ["create"]
}

After the process is complete, you might see a similar response:

Response
{
  "links": [{
    "l": "https://..."        // Download link
    // ...
  }]
}

Where "l" points to a fully ready, generated image based on the prompt and options you specified in the previous step.

Generating a new picture

Our API can generate a single picture with a single prompt at a time, but parallel processing is allowed and recommended. To generate multiple pictures, you just have to send multiple requests.

import json
import requests

api_url = "https://api.piktid.com/api"
access_token = "your_access_token"

# This is explained in more detail in the "Waiting for results" section
image_id = None
prompts = [
    "Half-body image of a 25-year-old White man, trimmed beard, wearing a casual leather jacket, relaxed pose, plain grey background.", 
    "Half-body image of a 70-year-old African woman, colorful traditional dress and headwrap, elegant pose, flat studio white background."
]
aspect_ratio = "16:9"
options= {
  "prompt_enhancement_flag": True,
  "seed": None
}

for prompt in prompts:
    data = requests.post(
        api_url + "/create/new",
        headers={"Authorization": "Bearer " + access_token},
        json={
            "id_image": image_id,
            "prompt": prompt,
            "aspect_ratio": aspect_ratio,
            "options": json.dumps(options)
        },
    ).json()

Generation options

The aspect_ratio parameter controls the aspect ratio of the generated images. It must be one of the following strings:

  • "1:2"
  • "9:16"
  • "2:3"
  • "3:4"
  • "1:1"
  • "4:3"
  • "3:2"
  • "16:9"
  • "2:1"

The image_id parameter is not required, but can be passed so all subsequent generations will be appended to the history (see /history) of the given image ID. The first notification for a generated image will contain a valid image ID that can be used for the following generations.

If image_id is left undefined, all new generated images will have a different image ID. To learn more about image IDs, see Image ID.

The options parameter in the request body must be formatted as a JSON-encoded string. It can contain the following parameters:

ParameterValuesDescription
prompt_enhancement_flagBooleanTurns on (true) or off (false) the prompt enhancer. It is responsible for making simple prompts generate higher quality images.
seedInteger numberSeed used for random number generation. Two requests with the same seed will have the same result. If undefined, it will be randomized server-side.

Waiting for results

Be careful!

Notifications are automatically deleted after exactly 10 minutes (600 seconds) from their creation.

Results are shared to clients via "notifications". Clients are expected to poll for notifications and handle them in the allowed time limit before they are automatically dismissed by the API.

import time
import requests
from pprint import pprint

api_url = "https://api.piktid.com/api"
access_token = "your_access_token"

image_id = "67762577..."


def process_notifications(response):
    for notification in response.get("notifications_list", []):
        if notification["name"] != "create":
            return None, None

        return notification["data"], notification["id"]

    return None, None


def delete_notification(notification_id):
    requests.delete(
        api_url + "/notification/delete_json",
        headers={"Authorization": "Bearer " + access_token},
        json={"id": notification_id},
    )


count = 0
# Loops for 10 minutes, even though generation generally
# takes less time.
for _ in range(600):
    response = requests.post(
        api_url + "/notification_by_name_json",
        headers={"Authorization": "Bearer " + access_token},
        json={"name_list": "create,error"},
    ).json()

    data, notification_id = process_notifications(response)
    if data is not None:
        # Delete the notification to avoid reading it again
        delete_notification(notification_id)

        print(f"Picture generated! Image ID: {data['id_image']}")
        link = data["links"][0]
        # The "l" field contains a static link to the final image
        print(link["l"])

        count += 1
        if count == len(prompts):
            break

    time.sleep(1)

PRO users can also disable watermark application on the final image.