How to integrate Imagen2 AI in NodeJS


Imagen is Google’s new state-of-the-art image generation AI model. In this post, we’ll look at how to integrate Imagen into a Node.js application to generate images from text prompts.

What’s great about Imagen2 is that anyone can access it without needing to apply for trusted tester access. This means it’s effectively generally available to the public already.

So you don’t need to go through an application process or be approved to generate images with Imagen2. As long as you have a Google Cloud account and authentication set up, you can start building imaginative applications powered by this new state-of-the-art AI today.

We’ll be using the Google Cloud AI Platform to access Imagen. So first we need to set up authentication and get an ID token:

import { JWT } from "google-auth-library";
import dotenv from "dotenv";

dotenv.config({override: true});

const client = new JWT({
  keyFile: "./google.json",
  scopes: [

const idToken = await client.authorize();

This uses a service account key file to get OAuth credentials and request an ID token we can use to call the Imagen API.

Next we set up the API endpoint and headers:

const API_ENDPOINT = "";
const IMAGEN_URL = `https://${API_ENDPOINT}/v1/projects/${process.env.GOOGLE_KEY}/locations/us-central1/publishers/google/models/imagegeneration:predict`;

const headers = {
  Authorization: `Bearer ${await getIdToken()}`,
  "Content-Type": "application/json",

To generate an image, we need to send a JSON payload with the prompt text:

const data = {
  instances: [
      prompt: "a cute baby sea otter",
  parameters: {
    sampleCount: 1,

We can then call the API and get the image data in Base64 encoding:

const response = await fetch(IMAGEN_URL, {
  method: "POST",
  body: JSON.stringify(data),  

const result = await response.json();

return Buffer.from(result?.predictions?[0]?.bytesBase64Encoded, "base64");

And that’s it! With just a few lines of code we can now generate images from Imagen right inside our Node.js applications.

Some ideas for using this:

  • Generative art
  • Automatically creating images for blog posts
  • Making profile pictures
  • And more!


Image description

Image description

Image description

Full story generated by my service (Manga TV) here:

Let me know in the comments if you end up building something cool with the Imagen2 API.

Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post

How Often Should You (or Your Company) Blog? [New Data]

Next Post

Pushing Cloudflare Worker logs to Grafana Loki

Related Posts