import fs from "fs"; const options = { method: "POST", headers: { Accept: "application/json

import fs from "fs";

const options = {
method: "POST",
headers: {
Accept: "application/json",
"content-type": "application/json",
Authorization: "Bearer YOUR_API_KEY"
},
body: JSON.stringify({
samples: 1,
height: 1024,
width: 1024,
steps: 40,
cfg_scale: 5,
textprompts: [
{
"text": "A painting of a cat",
"weight": 1
},
{
"text": "a dog barking",
"weight": 1
},
{
"text": "logo, ",
"weight": -1
}
],
})
};

const response = await fetch(
"https://api.stability.ai/v1/generation/stable-diffusion-xl-1024-v1-0/text-to-image",
options
)

if (!response.ok) {
throw new Error(
Non-200 response: ${await response.text()}
)
}

const responseJSON = await response.json()

responseJSON.artifacts.forEach((image, index) => {
fs.writeFileSync(
`./out/txt2img
${image.seed}.png`,
Buffer.from(image.base64, 'base64')
)
});
Was this page helpful?