Pollinations
A Node.js package that allows you to easily generate images using Pollinations's free API.
Installation
Install pollinations to your project with npm:
npm install pollinations
Example Usage
Integrating pollinations to a Discord bot using discord.js.
npm install discord.js
const { Client, GatewayIntentBits, AttachmentBuilder } = require("discord.js");
const Pollinations = require("pollinations")
const pollinations = new Pollinations()
const Bot = new Client({
intents: [
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.DirectMessages
]
});
Bot.on("messageCreate", async (message) =>{
if(message.bot) return;
if(!message.content) return;
if(message.content.toLowerCase().startsWith("!generate")){
const prompt = message.content.substring("!generate".length).trim();
if(prompt.length === 0) return message.reply(":x: Please provide a prompt.");
message.react("✅")
pollinations.generate({
prompt: prompt
})
.then(result =>{
const imagesBuffers = []
result.images.forEach(image =>{
imagesBuffers.push(
new AttachmentBuilder(image, {
name: "pollinations-image.jpg
})
)
})
message.reply({
content: "Here is your image",
files: imagesBuffers
})
})
.catch(error =>{
return message.reply(":x: Something went wrong.");
})
}
})
Bot.login("VERY_SECRET_TOKEN")
Documentation
Initialization
const Pollinations = require("pollinations")
const pollinations = new Pollinations(defaultParameters)
Parameter | Type | Default | Description |
---|
images | number | 1 | Default number of images to generate when using the generate() function |
width | number | 512 | Default width for images in pixels |
height | number | 512 | Default height for images in pixels |
seed | number | -1 (-1 indicates random) | Default seed used to generate images |
nologo | number | false | Whether the images should have the Pollinations logo or not |
generate()
pollinations.generate(generateParameters).then(results)
The pollinations.generate()
returns images by sending HTTPS requests to the official Pollinations website; It takes an Object
parameter and return an Object
.
Parameter | Type | Default | Description |
---|
prompt | string | N/A | (REQUIRED) Description of the image(s) to generate |
images | number | defaultParameters or 1 | Number of images to generate |
width | number | defaultParameters or 512 | Width for images in pixels |
height | number | defaultParameters or 512 | Height for images in pixels |
seed | number | defaultParameters or -1 | Seed to use to generate images |
nologo | number | defaultParameters or false | Whether the images should include the Pollinations logo or not |
Name | Type | Description |
---|
Object.requestId | string | Unique 36-character alphanumeric string you can use to identify the generate request |
Object.prompt | string | The prompt used for the generation |
Object.width | number | The width used for the generation |
Object.height | number | The height used for the generation |
Object.seed | number | The seed used for the generation |
Object.nologo | boolean | Whether the images include the Pollinations logo or not |
Object.expectedImages | number | Requested number of images to generate |
Object.errors | number | How many errors happened during the process |
Object.images | array | Array of buffers |
Object.isComplete | boolean | true if the number of images in the images array is the same as expectedImages |