<aside> đź””
We’re working on a new API with an easier-to-use schema and support for human ask nodes. You can see the proposed schema here.
Please note the proposed API is in alpha and is not suitable for production use. The format may change and there may be downtime without warning. You should use the API documented on this page for production use cases. t
</aside>
First get an API key by first visiting the settings page.
To access the settings page you can click the user avatar in the top right corner
Then select the “API Keys” tab on the left hand side
After you’ve published an app you can access its API in the API tap of that app’s “Deployments” page.
There you’ll find an endpoint that looks like the following:
<https://app.wordware.ai/api/released-app/><app_id>/run
import json
import requests
def main():
app_id = "YOUR_APP_ID"
api_key = "YOUR_API_KEY"
# Execute the prompt
r = requests.post(f"<https://app.wordware.ai/api/released-app/{app_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>",
},
"version": "^1.0"
}
},
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()
const API_KEY = "YOUR_API_KEY"
const appId = "YOUR_APP_ID";
async function main() {
console.log("Running app", appId);
// Run the prompt, streaming the outputs as they're generated
const r = await fetch(
`https://app.wordware.ai/api/released-app/${appId}/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>",
},
},
version: "^1.0",
}),
headers: {
Authorization: `Bearer ${API_KEY}`,
},
},
);
if (!r.ok) {
console.error("Run failed", await r.json());
throw Error(`Run failed ${r.status}`)
}
const reader = r.body.getReader();
const decoder = new TextDecoder();
let buffer = [];
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();