Create Stunning Vector Images with Recraft AI's Cognitive Actions

21 Apr 2025
Create Stunning Vector Images with Recraft AI's Cognitive Actions

In the world of digital design, vector images play a crucial role due to their scalability and versatility. The Recraft AI's Cognitive Actions provide a powerful toolset for developers to generate vector images efficiently. With the Generate Vector Image action, you can create customized vector illustrations and icons in various styles and sizes, tailored to your specific needs. This article explores how to leverage this action for your applications.

Prerequisites

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

  • An API key for the Cognitive Actions platform to authenticate your requests.
  • Familiarity with JSON format, as the input and output will be in this structure.

Conceptually, to authenticate, you'll typically include the API key in the headers of your requests.

Cognitive Actions Overview

Generate Vector Image

The Generate Vector Image action allows you to create affordable and fast vector images using specified styles and sizes. This operation enables customization of image aspect ratios and styles, providing a diverse range of vector illustrations and icons.

Input

The input for this action requires a JSON object with the following fields:

  • prompt (required): A string that describes the desired content of the image.
  • size (optional): Specifies the dimensions of the generated image, with a default value of 1024x1024.
  • style (optional): Defines the visual style of the generated image, defaulting to vector_illustration.
  • aspectRatio (optional): Determines the width-to-height ratio of the image. If set, it overrides the size parameter, with a default value of Not set.

Example Input:

{
  "size": "1024x1024",
  "style": "vector_illustration",
  "prompt": "hand holding a phone, foliage in the back"
}

Output

Upon successful execution, the action returns a URL pointing to the generated vector image in SVG format. Here is an example of what the output might look like:

Example Output:

https://assets.cognitiveactions.com/invocations/fdf28e5c-4c24-4695-88ae-ba1b37ed1d9d/c060031b-525e-4776-8e51-aec0b5d30772.svg

Conceptual Usage Example (Python)

Here’s a conceptual example of how you might call the Generate Vector Image 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 = "d26a430f-3873-41ca-81ad-2c5460503fc1" # Action ID for Generate Vector Image

# Construct the input payload based on the action's requirements
payload = {
    "size": "1024x1024",
    "style": "vector_illustration",
    "prompt": "hand holding a phone, foliage in the back"
}

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 example, replace YOUR_COGNITIVE_ACTIONS_API_KEY with your actual API key. The action ID and input payload should be structured as shown to ensure that the request is properly formatted. Note that the endpoint URL and request structure are illustrative.

Conclusion

The Generate Vector Image action from Recraft AI's Cognitive Actions provides an efficient way to create customized vector graphics for your applications. By leveraging this action, developers can enhance their applications with unique and scalable vector images. Consider exploring additional use cases, such as creating icons for user interfaces or illustrations for marketing materials. Happy coding!