Skip to content

Instantly share code, notes, and snippets.

@Andrej123456789
Created January 15, 2023 14:49
Show Gist options
  • Save Andrej123456789/c197b7c87039edee40af7b9461f5a646 to your computer and use it in GitHub Desktop.
Save Andrej123456789/c197b7c87039edee40af7b9461f5a646 to your computer and use it in GitHub Desktop.
import { Configuration, OpenAIApi } from "openai";
import { writeFileSync } from "fs";
async function Generate(prompt) {
const configuration = new Configuration({
apiKey: "your api key",
});
const openai = new OpenAIApi(configuration);
const result = await openai.createImage({
prompt,
n: 1,
size: "1024x1024",
user: "Andrej",
});
const url = result.data.data[0].url;
console.log(url);
const imgResult = await fetch(url);
const blob = await imgResult.blob();
const buffer = Buffer.from(await blob.arrayBuffer());
writeFileSync(`./img/${Date.now()}.png`, buffer);
}
/* Main function */
// Generate("monorail");
Generate("your prompt goes here");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment