The open source package can be found here:
Installation
You can clone/fork the repo and copy the plugin-bonsai/
directory into your packages/
directory.
Exports
These are the main exports available from the package
import {
GenerationService,
Template,
createSmartMedia
} from "@elizaos/plugin-bonsai";
GenerationService
: create content from a prompt and template
Template
: the types of templates available
createSmartMedia
: as an authenticated Lens account, post your content to the Bonsai feed
Guide: How to create content for a Clanker
Here, we'll show an example on how to generate an image using a template on Bonsai, and paying via USDC on Base.
const response = await fetch(`https://www.clanker.world/api/get-clanker-by-address?address=${tokenAddress}`, {
headers: { 'x-api-key': process.env.CLANKER_API_KEY as string }
});
const token = await response.json();
2. Initialize the generation service with a viem Account
This wallet must have USDC on Base to pay for generations
import { GenerationService, Template, GenerationResponse } from "@elizaos/plugin-bonsai";
const account = privateKeyToAccount(process.env.EVM_PRIVATE_KEY as `0x${string}`);
const generationService = new GenerationService(account, "base", process.env.BASE_RPC_URL as string);
3. Send the generation request
const template = Template.IMAGE; // Template.STORY | Template.VIDEO
const subTemplateId = "wall_st_bets"; // "animal_fruit" | "animal_brand"
const prompt = `${token.data.symbol} on the moon`;
// optionally enhance your prompt
const enhancedPrompt = await generationService.enhancePrompt({ prompt, template });
// this will trigger the x402 payment flow before the generation request is processed
const generationResponse = await generationService.create({
prompt: enhancedPrompt,
template,
image: token.data.img_url, // use the token image in the generation
subTemplateId,
});
4. Parse the generation data and do whatever you want with it
interface GenerationResponse {
generation: {
text?: string;
image?: string; // base64
video?: {
buffer: number[],
mimeType: string,
size: number
}
}
templateData: any;
}
// parse the generation content and template metadata
const { generation, templateData } = generationResponse as GenerationResponse;