DRAWTEXT API Endpoint

Version 2.197 (Release Notes ↗)

Description

Draw text on the top, center, or bottom of an image using the DRAWTEXT API endpoint. Perfect for developers and creators looking to enhance image content. Draw some text on Top, Center or Bottom of a given image.

HTTP Methods

GET, POST

HTTP Parameters

Required

Fields Type Description
img URL Input media URL. If uploading directly from your app, submit a multipart/form-data POST request.
top String Text to render at top center of the image.
center String Text to render at center of the image.
bottom String Text to render at bottom center of the image.
key String Your PixLab API Key ↗. Alternatively, embed your key in the WWW-Authenticate: HTTP header.

Optional

Fields Type Description
blob Boolean Returns image binary data when true (default: JSON response with output URL).
font String Font name (default: Impact). Available options: impact, college, bold, square, arial, mario, vera, owl, wolf, gym, dotty, meth, puppy. Register custom fonts via the dashboard ↗.
cap Boolean Capitalizes all text when enabled.
size Integer Font size in points.
color String Font color (default: white). Accepts hex codes (e.g., #cef45f).
strokecolor String Stroke/border color (e.g., black or hex code).
strokewidth Float Stroke width (default: 2 when strokecolor is set).
strokeopacity Float Stroke opacity (default: 0.9 when strokecolor is set).

POST Request Body

Supported Content-Type:

  • multipart/form-data (direct file upload)
  • application/json (requires pre-uploaded media via store)

Refer to the REST API code samples for implementation examples.

HTTP Response

Returns application/json if the optional blob parameter is not set.

This endpoint returns a JSON Object after each call only if the optional blob parameter is not set. Otherwise the image binary contents is returned instead. The following are the JSON fields returned in response body:

Fields Type Description
status Integer Status code 200 indicates success, any other code indicates failure.
link URL Link to the media output which is usually stored on the pixlab.xyz storage server unless you set your own S3 keys (refer to your dashboard ↗ on how to do that).
id String Unique media ID.
error String Error message if status != 200.

Code Samples


import requests


def generate_meme(
    img_url: str,
    top_text: str,
    bottom_text: str,
    capitalize: bool = True,
    stroke_color: str = "black",
    api_key: str = "PIXLAB_API_KEY",
) -> str:
    """Generate a meme by adding text to an image using PixLab API.
    
    Args:
        img_url: URL of the base image.
        top_text: Text to be displayed at the top.
        bottom_text: Text to be displayed at the bottom.
        capitalize: Whether to capitalize the text.
        stroke_color: Color of the text stroke.
        api_key: PixLab API key.
    
    Returns:
        URL of the generated meme.
    
    Raises:
        Exception: If API request fails.
    """
    params = {
        "img": img_url,
        "top": top_text,
        "bottom": bottom_text,
        "cap": capitalize,
        "strokecolor": stroke_color,
        "key": api_key,
    }

    try:
        response = requests.get("https://api.pixlab.io/drawtext", params=params)
        response.raise_for_status()
        data = response.json()

        if data["status"] != 200:
            raise Exception(data.get("error", "Unknown error occurred"))

        return data["link"]

    except requests.exceptions.RequestException as e:
        raise Exception(f"API request failed: {e}")


if __name__ == "__main__":
    try:
        meme_url = generate_meme(
            img_url="https://pixlab.io/images/jdr.jpg",
            top_text="someone bumps the table",
            bottom_text="right before you win",
        )
        print(f"Meme: {meme_url}")
    except Exception as e:
        print(f"Error: {e}")
← Return to API Endpoint Listing