Official AIMG API client for Node.js. Allows you to generate dynamic images (PNG/GIF) using HTML templates. ✨
npm install aimg.js
# or
pnpm install aimg.js
# or
yarn add aimg.js
First, import the client and initialize it with your API key:
// filepath: example.js
const { AIMGClient } = require("aimg.js");
// or with ES Modules:
// import { AIMGClient } from "aimg.js";
const client = new AIMGClient({
apiKey: "YOUR_API_KEY", // Enter your API Key here
templatesPath: "./templates" // (Optional) Path to your templates folder, default: process.cwd()/templates
});
Renders an image using the specified template name. It reads the files (template.aimg.html
, session.aimg.json
, data.aimg.json
, animation.aimg.json
) from the template folder and sends them to the AIMG API.
You can see example templates in the templates folder.
// filepath: exampleRenderTemplate.js
const { AIMGClient } = require("aimg.js");
const fs = require("fs");
const client = new AIMGClient({ apiKey: "YOUR_API_KEY" });
async function renderMyTemplate() {
try {
// Render the 'level' template with default data.aimg.json
const result = await client.renderTemplate("level");
// Or render with custom data
const customData = {
user: {
name: "Custom User",
displayName: "Custom User | 99",
avatarUrl: "https://example.com/avatar.png",
avatarColor: "#ff0000"
},
levels: [ /* ... level data ... */ ]
};
const resultWithData = await client.renderTemplate("level", customData);
console.log("Render successful!", result.filename);
// Example: Save the image to a file
fs.writeFileSync(result.filename, result.buffer);
console.log(`Image saved as ${result.filename}.`);
} catch (error) {
console.error("Render error:", error);
}
}
renderMyTemplate();
Renders an image by directly providing HTML content, configuration, and session settings.
// filepath: exampleRenderCustom.js
const { AIMGClient } = require("aimg.js");
const fs = require("fs");
const client = new AIMGClient({ apiKey: "YOUR_API_KEY" });
async function renderCustomHtml() {
const htmlContent = `
<!DOCTYPE html>
<html>
<head><title>Custom</title></head>
<body><h1>Hello, <%= it.name %>!</h1></body>
</html>
`;
const config = {
data: { name: "World" },
// Animation settings if needed
// animation: { ... }
};
const sessionConfig = {
type: "NoScriptStatic", // or "NoScriptAnimated"
// Profiles if needed
profiles: []
// profiles: [{ name: "TailwindCSS" }]
};
try {
const result = await client.render(htmlContent, config, sessionConfig);
console.log("Custom render successful!", result.filename);
// Example: Save the image to a file
fs.writeFileSync(result.filename, result.buffer);
console.log(`Image saved as ${result.filename}.`);
} catch (error) {
console.error("Custom render error:", error);
}
}
renderCustomHtml();
Each subfolder within the templatesPath
represents a template:
-
template.aimg.html
: The HTML structure of the image. Supports data injection using EJS-like<%= ... %>
syntax (via theit
variable). -
session.aimg.json
: Render session settings (RestSessionInstance
). Specifies thetype
(NoScriptStatic
orNoScriptAnimated
) and theprofiles
to use (e.g.,TailwindCSS
,Unovis
,GSAP
). -
data.aimg.json
(Optional): Default data to be used in the template. This file is used if thedata
parameter is not provided in therenderTemplate
call. -
animation.aimg.json
(Optional): Contains animation definitions iftype
is set toNoScriptAnimated
insession.aimg.json
.
Check the templates folder for example templates.
Creates a new client instance.
-
options
:AIMGClientOptions
-
apiKey
(string, required): Your AIMG API key. -
rest?
:RestOptions
(Optional) Additional settings for the REST client (e.g.,baseUrl
,timeout
,headers
). -
templatesPath?
: (string, Optional) Path to the folder containing templates. Default:path.join(process.cwd(), "templates")
.
-
Renders a template.
-
name
(string): The name of the template folder withintemplatesPath
. -
data?
(Record<string, any>, Optional): Custom data to send to the template. If not specified, the template'sdata.aimg.json
file is used. -
Returns:
Promise<{ buffer: Buffer, filename: string, content_type: "image/png" | "image/gif", statistics: any }>
Renders custom HTML content.
-
html
(string): The HTML content to render. -
config
:RestRenderSessionOptions['config']
-
data
(Record<string, any>): Data to inject into the HTML. -
animation?
: Animation settings (ifsessionConfig.type
is animated).
-
-
sessionConfig
:RestSessionInstance
Specifies the session type and profiles. -
Returns:
Promise<{ buffer: Buffer, filename: string, content_type: "image/png" | "image/gif", statistics: any }>
To build the project:
npm run build
This command compiles the TypeScript source code from src into JavaScript in the dist
folder.
This project is licensed under the GNU General Public License v3.0.