Get an API key

You can get your API key at this link, or watch the video below:

How to generate an API key

How to generate an API key

Trigger the API

There are two API endpoints /prompt/<prompt_id>/describe and /prompt/<prompt_id>/run.

Examples

cURL

Describe

curl <https://app.wordware.ai/api/prompt/><prompt_id>/describe -H "Authorization: Bearer <API_KEY>"

Run

curl -X POST <https://app.wordware.ai/api/prompt/efb95b61-b107-4677-847a-24499db5e2cc/run> -H "Authorization: Bearer <API_KEY>" -d '{"inputs": {"<input_1_key>": "<input_1_value>"}}'

Python

import json
import requests

def main():
    prompt_id = "YOUR_PROMPT_ID"
    api_key = "YOUR_API_KEY"

    # Describe the prompt (shows just the inputs for now)
    r1 = requests.get(f"<https://app.wordware.ai/api/prompt/{prompt_id}/describe>",
                      headers={"Authorization": f"Bearer {api_key}"})
    print(json.dumps(r1.json(), indent=4))

    # Execute the prompt
    r = requests.post(f"<https://app.wordware.ai/api/prompt/{prompt_id}/run>",
                      json={
                          "inputs": {
                              "topic": "sugary cereal",
                              # Image inputs have a different format and require a publicly accessible URL
															"image": {
                                  "type": "image",
                                  "image_url": "<https://i.insider.com/602ee9ced3ad27001837f2ac>",
                              },
                          }
                      },
                      headers={"Authorization": f"Bearer {api_key}"},
                      stream=True
                      )

    # Ensure the request was successful
    if r.status_code != 200:
        print("Request failed with status code", r.status_code)
        print(json.dumps(r.json(), indent=4))
    else:
        for line in r.iter_lines():
            if line:
                content = json.loads(line.decode('utf-8'))
                value = content['value']
                # We can print values as they're generated
                if value['type'] == 'generation':
                    if value['state'] == "start":
                        print("\\nNEW GENERATION -", value['label'])
                    else:
                        print("\\nEND GENERATION -", value['label'])
                elif value['type'] == "chunk":
                    print(value['value'], end="")
                elif value['type'] == "outputs":
                    # Or we can read from the outputs at the end
                    # Currently we include everything by ID and by label - this will likely change in future in a breaking
                    # change but with ample warning
                    print("\\nFINAL OUTPUTS:")
                    print(json.dumps(value, indent=4))

if __name__ == '__main__':
    main()

JavaScript/TypeScript

const API_KEY = "YOUR_API_KEY"
const promptId = "YOUR_PROMPT_ID";

async function main() {
  // First describe the prompt to see which inputs are required
  const describeResponse = await fetch(
    `https://app.wordware.ai/api/prompt/${promptId}/describe`,
    {
      headers: {
        Authorization: `Bearer ${API_KEY}`,
      },
    }
  );

  console.log(await describeResponse.json());

  // Then run the prompt, streaming the outputs as they're generated
  const runResponse = await fetch(
    `https://app.wordware.ai/api/prompt/${promptId}/run`,
    {
      method: "post",
      body: JSON.stringify({
        inputs: {
          topic: "Sugary cereal",
          // Image inputs have a different format and require a publicly
          // accessible URL
					image: { 
            type: "image",
            image_url: "<https://i.insider.com/602ee9ced3ad27001837f2ac>",
          },
        },
      }),
      headers: {
        Authorization: `Bearer ${API_KEY}`,
      },
    }
  );

  const reader = runResponse.body.getReader();

  const decoder = new TextDecoder();
  let buffer: string[] = [];

  try {
    while (true) {
      const { done, value } = await reader.read();

      if (done) {
        return;
      }

      const chunk = decoder.decode(value);

      for (let i = 0, len = chunk.length; i < len; ++i) {
        const isChunkSeparator = chunk[i] === "\\n";

        // Keep buffering unless we've hit the end of a data chunk
        if (!isChunkSeparator) {
          buffer.push(chunk[i]);
          continue;
        }

        const line = buffer.join("").trimEnd();

        // This is the chunk
        const content = JSON.parse(line);
        const value = content.value;

// Here we print the streamed generations
        if (value.type === "generation") {
          if (value.state === "start") {
            console.log("\\nNEW GENERATION -", value.label);
          } else {
            console.log("\\nEND GENERATION -", value.label);
          }
        } else if (value.type === "chunk") {
          process.stdout.write(value.value ?? "")
        } else if (value.type === "outputs") {
          console.log(value);
        }

        buffer = [];
      }
    }
  } finally {
    reader.releaseLock();
  }
}

main();