Generate Stunning Images with recraft-ai/recraft-20b Cognitive Actions

25 Apr 2025
Generate Stunning Images with recraft-ai/recraft-20b Cognitive Actions

In today's digital landscape, the ability to create high-quality images quickly and affordably is a game-changer for developers. The recraft-ai/recraft-20b Cognitive Actions provide a powerful API that allows you to generate images with customizable sizes, styles, and aspect ratios effortlessly. With just a few lines of code, you can integrate this functionality into your applications, enhancing user experiences and driving engagement.

Prerequisites

Before diving into the Cognitive Actions, ensure you have the following:

  • An API key for the Cognitive Actions platform, which you will use for authentication.
  • Basic knowledge of JSON and API requests, as you will be sending structured data to the Cognitive Actions endpoint.

For authentication, you typically include your API key in the request headers as a Bearer token.

Cognitive Actions Overview

Generate Fast Affordable Images

The Generate Fast Affordable Images action allows you to produce images based on textual descriptions. You can customize the image's size, style, and aspect ratio to suit your needs.

  • Category: Image Generation

Input

The input for this action requires the following fields:

  • prompt (required): A textual description guiding the image generation process.
  • size (optional): Specifies the image dimensions. If you set an aspect ratio, this field will be ignored. Default is 1024x1024.
  • style (optional): Defines the visual style of the generated image. The default is realistic_image.
  • aspectRatio (optional): Specifies the aspect ratio of the image. This field overrides the size if set.

Here’s an example of how to structure your input JSON payload:

{
  "size": "1024x1024",
  "style": "realistic_image/b_and_w",
  "prompt": "a portrait photo"
}

Output

The action will return a URL pointing to the generated image. Here’s an example of a typical output:

https://assets.cognitiveactions.com/invocations/b46babbe-3a62-40b2-a7a7-bed029f2d8a9/980ec67b-95e0-45ec-b95f-c9b540cd3826.webp

If there are any errors in the request, the response may contain error messages or status codes.

Conceptual Usage Example (Python)

Here’s a conceptual example of how you might call the Generate Fast Affordable Images action using Python:

import requests
import json

# Replace with your Cognitive Actions API key and endpoint
COGNITIVE_ACTIONS_API_KEY = "YOUR_COGNITIVE_ACTIONS_API_KEY"
COGNITIVE_ACTIONS_EXECUTE_URL = "https://api.cognitiveactions.com/actions/execute" # Hypothetical endpoint

action_id = "e319270c-f85c-49af-85b6-523e7d45f46b" # Action ID for Generate Fast Affordable Images

# Construct the input payload based on the action's requirements
payload = {
    "size": "1024x1024",
    "style": "realistic_image/b_and_w",
    "prompt": "a portrait photo"
}

headers = {
    "Authorization": f"Bearer {COGNITIVE_ACTIONS_API_KEY}",
    "Content-Type": "application/json"
}

try:
    response = requests.post(
        COGNITIVE_ACTIONS_EXECUTE_URL,
        headers=headers,
        json={"action_id": action_id, "inputs": payload} # Hypothetical structure
    )
    response.raise_for_status() # Raise an exception for bad status codes (4xx or 5xx)

    result = response.json()
    print("Action executed successfully:")
    print(json.dumps(result, indent=2))

except requests.exceptions.RequestException as e:
    print(f"Error executing action {action_id}: {e}")
    if e.response is not None:
        print(f"Response status: {e.response.status_code}")
        try:
            print(f"Response body: {e.response.json()}")
        except json.JSONDecodeError:
            print(f"Response body: {e.response.text}")

In this code snippet, replace the placeholder for the API key and endpoint with your actual credentials. The payload variable is constructed based on the input schema requirements, and the action ID is specified for the image generation action.

Conclusion

The Cognitive Actions from recraft-ai/recraft-20b make it easy for developers to generate images tailored to specific requirements without needing extensive resources or complex setup. By leveraging these actions, you can enhance your applications with rich visual content, improving user engagement and satisfaction. Start integrating these capabilities into your projects today and see the difference it makes!