API
Access your Wordix app via the API.
JavaScript Implementation
const API_KEY = "YOUR_API_KEY";
const appId = "YOUR_APP_ID";
async function main() {
// Run the prompt, streaming the outputs as they're generated
const r = await fetch(
`https://app.wordix.so/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: "",
},
},
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();
Last updated on