Running a Magpai workflow via API

You need a paid Magpai account in order to access this features. Click here to upgrade your account.

The high level steps are as follows:

  1. Create an API key in your profile Create API key in your profile
  2. Create your Workflow and get the Workflow ID from the url (e.g. gKT7k6zt666F4oucrwXk)
  3. Send a POST request to https://magpai.app/api/v1/workflow/run with the inputs you want to use.

Credits will be used from the account linked to the API token.

Inputs

Inputs are provided by the "inputs" dictionary passed to the body of the POST request. The keys should correspond to the names of the input nodes in the workflow.

Types are checked based on the input type in the workflow.

Images need to be hosted urls. We hope to soon have support for submitting base64 encoded strings for images.

Outputs

The response object of the POST request will look something like the following:

{
  message: 'Job completed!',
  jobId: 'sJ2k71ESAIgmaqVSkJrr',
  outputs: [
    {
      name: 'output',
      type: 'image',
      value: 'https://storage.googleapis.com/magpai-c017e.appspot.com/storage/job/sJ2k71ESAIgmaqVSkJrr/upload/public/output.png'
    }
  ]
}

These outputs correspond to the output nodes in your Workflow.

Webhook

When running a workflow via the API, you can send a POST endpoint you wish to be triggered when the job ends.

Simply add the webhook field to the body of your request.

The webhook should handle POST routes. You will receive an object with a jobId field, and an outputs field.

This is very experimental, and we hope to expand this to return more information, and be filterable.

Code Snippets

Javascript

Here is a JavaScript sample of how to submit a job.

const WorkflowID = "gKT7k6zt666F4oucrwXk"; // Replace this with your Workflow ID
const API_KEY = "INSERT_API_KEY_HERE";
fetch("https://magpai.app/api/v1/workflow/run", {
  method: "POST",
  cache: "no-cache",
  headers: {
    "Content-Type": "application/json",
    Authorization: `Token ${API_KEY}`,
  },
  body: JSON.stringify({
    workflowId: WorkflowID,
    webhook: "https://example.com/", // This is optional, and will be triggered after the job has successfully completed
    inputs: {
      Prompt: "Polaroid photo of winnie the pooh",
    },
  }),
})
  .then((x) => x.json())
  .then((x) => {
    console.log(x);
  });

Python

import requests
import json

WorkflowID = "gKT7k6zt666F4oucrwXk"  # Replace this with your Workflow ID
API_KEY = "INSERT_API_KEY_HERE"

url = "https://magpai.app/api/v1/workflow/run"
headers = {
    "Content-Type": "application/json",
    "Authorization": f"Token {API_KEY}"
}
data = {
    "workflowId": WorkflowID,
    "webhook": "https://example.com/", # This is optional, and will be triggered after the job has successfully completed
    "inputs": {
        "Prompt": "Polaroid photo of winnie the pooh"
    }
}

response = requests.post(url, headers=headers, data=json.dumps(data))
response_json = response.json()

print(response_json)

img = response_json['outputs'][0]['value']

Caveats

  • If your Workflow takes longer than 9 minutes to complete, your request will timeout, we are working to come up with a solution to this
  • No progress is reported yet, we hope to have streaming support from the request soon.
© 2024 Magpai