DEV Community

Cover image for How to integrate Imagen2 AI in NodeJS
Max Sveshnikov
Max Sveshnikov

Posted on

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: [
    "https://www.googleapis.com/auth/cloud-platform",
  ],  
});

const idToken = await client.authorize();


Enter fullscreen mode Exit fullscreen mode

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 = "us-central1-aiplatform.googleapis.com";
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",
};


Enter fullscreen mode Exit fullscreen mode

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,
  },
};


Enter fullscreen mode Exit fullscreen mode

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



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

const result = await response.json();

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


Enter fullscreen mode Exit fullscreen mode

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!

Examples:

Image description

Image description

Image description

Full story generated by my service (Manga TV) here: https://mangatv.shop/story/dune-battle-fremens-vs-sarduckars

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

Top comments (3)

Collapse
 
sayon_phantasm profile image
Sayonara Phantasm • Edited
const client = new JWT({
  keyFile: "./google.json",
  scopes: [
    "https://www.googleapis.com/auth/cloud-platform",
  ],  
});
Enter fullscreen mode Exit fullscreen mode

what inside of file google.json bro ?
i have problem with auth

\edit:
i just download json file on APIs & Service > credentials, but got error getIdToken is not defined, what is that?

i try use idToken.access_token but still failed, help T_T

Collapse
 
frederickk profile image
kenfrederick • Edited

for anyone else that stumbles across this, here's how to fix the missing getIdToken is not defined error:

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

dotenv.config({override: true});

const client = new JWT({
  keyFile: './credentials.json',
  scopes: ['https://www.googleapis.com/auth/cloud-platform'],
});

const idToken = await client.authorize();

const headers = {
  // This is the update that fixes the missing code
  Authorization: `Bearer ${idToken.access_token}`,
  'Content-Type': 'application/json',
};

// From here onward it's the same as the original post
const response = await fetch(IMAGEN_URL, {
  method: "POST",
  headers,
  body: JSON.stringify(data),  
});

const result = await response.json();

return Buffer.from(result?.predictions?[0]?.bytesBase64Encoded, "base64");
const data = {
  instances: [
    { 
      prompt: "a cute baby sea otter",
    },
  ],
  parameters: {
    sampleCount: 1,
  },
};
Enter fullscreen mode Exit fullscreen mode
Collapse
 
pulkit_girdhar profile image
Pulkit Girdhar

Hey,
Can you share your repo or the basic idea.
I am new to backend and want to make use of it but don't have much knowledge as what goes into credentials.json and all