Node.js Image Generation API: Quick Start Guide

Node.js Image Generation API: Quick Start Guide

How to integrate and save imejisio api response as a file in nodejsHow to Integrate and Save Imejis.io API Response as a File in Node.js

In this tutorial, we will explore how to integrate the Imejis.io API in a Node.js application and save the API response as a file. If you're new to image generation APIs, start with our beginner guide to generating images with an API. Imejis.io provides a platform for customizable and interactive designs that can be easily integrated into your projects. By following these steps, you will learn how to make a request to the Imejis.io API and save the response as a file on your local machine using Node.js.

PrerequisitesPrerequisites

Before we proceed with the integration, ensure that you have the following prerequisites:

  1. Node.js installed on your local machine.
  2. A valid API key from Imejis.io. If you don't have one, sign up on their website (https://www.imejis.io) and obtain your API key.
  3. A design ID from Imejis.io. Select or create a design on the Imejis.io platform and make a note of its unique ID.

Let's get started with the integration process:

Note: You can use nested objects, flattened keys with dot notation (e.g., image.opacity, image.borderColor), or combine both formats in a single request for maximum flexibility. You don't have to pass all properties - only send the properties you want to update or change.

const axios = require("axios")
const fs = require("fs")
const path = require("path")
 
const DESIGN_ID = "YOUR_DESIGN_ID"
const YOUR_API_KEY = "YOUR_API_KEY"
 
const createAndSaveFile = async () => {
  // You can combine nested objects and flattened keys in the same request
  let data = JSON.stringify({
    text: {
      text: "Text here",
      color: "#000000",
      backgroundColor: "transparent",
      textBackgroundColor: "transparent",
      borderColor: "transparent",
      strokeColor: "transparent",
    },
    "image.image": "",
    "image.opacity": 1,
    "image.borderColor": "transparent",
  })
 
  let config = {
    method: "post",
    maxBodyLength: Infinity,
    url: `https://render.imejis.io/v1/${DESIGN_ID}`,
    headers: {
      "dma-api-key": YOUR_API_KEY,
      "Content-Type": "application/json",
    },
    data: data,
  }
 
  try {
    const response = await axios({
      ...config,
      responseType: "arraybuffer",
    })
    const filePath = path.join(__dirname, "output.png")
    fs.writeFileSync(filePath, response.data)
    console.log("File saved successfully.")
  } catch (error) {
    console.error("Error integrating Imejis.io API:", error.message)
  }
}
 
createAndSaveFile()

Replace 'YOUR_DESIGN_ID' with the actual design ID you obtained from the Imejis.io platform, and 'YOUR_API_KEY' with your valid API key.

Save the above code in a file named index.js.

To run the integration, open your terminal or command prompt, go to the project directory, and execute the following command:

node index.js

If everything is set up correctly, the code will make a POST request to the Imejis.io API with the provided design ID and API key. The API response, which is an image file in this case, will be saved as output.png in the project directory.

That's it! You have successfully integrated the Imejis.io API and saved the response as a file in your Node.js application. For a React-based approach, see our React image generation component guide, or check out the Next.js dynamic image generation tutorial for server-side rendering use cases. You can also generate images from JSON data for more dynamic workflows.

Frequently Asked Questions

You need Node.js installed, an API key from your image generation provider, and a design or template ID. The axios and fs packages handle HTTP requests and file saving.

Most APIs return PNG by default for high-quality output. Some also support JPEG for smaller file sizes and WebP for optimized web delivery.

Use async calls with await or promises. Image rendering takes time, and async requests prevent your application from blocking while waiting for the API response.

Wrap your API call in a try-catch block. Check the error status code for issues like invalid API keys (401), missing templates (404), or rate limits (429), and log the error message.

Yes, most APIs enforce rate limits based on your plan. Check your provider's docs for requests-per-minute limits and implement retry logic with backoff for production use.