Upload One Image to AvatarLookup from Node.js
Send one image to AvatarLookup with Node.js multipart FormData, then handle HTTP, application errors, category and optional estimates.

A server-side Node.js client for uploading one image and reading its analysis result safely.
Use a server-side Node.js request with multipart FormData: add service_type=image_profile and the image as file, then send it to AvatarLookup’s image profile endpoint with X-API-Key. Check the network outcome, HTTP status and response code before reading the returned category and optional estimates.
Check the local file before creating the request
Our image profile service analyzes the picture you upload. It accepts JPEG, PNG, WebP and GIF images up to 2 MB. For a single-file client, reject a missing, empty, oversized or unsupported file before sending a paid request. The extension and MIME mapping in the example below are a simple local preflight for files your server controls; for untrusted uploads, inspect actual file content as part of your own intake validation as well.
Keep the API key on your server. A browser upload can reach your backend, but the secret must not be embedded in client-side JavaScript or passed to the browser. This tutorial starts from a local file already available to the Node.js process. It does not implement the separate browser-to-backend authorization flow.
The request has no account identifier input. The image itself is the input, and the returned identifier is a content-derived fingerprint.
Build and send one multipart request in Node.js
The current API reference specifies POST /api/v1/image/profile, the X-API-Key header, and two multipart fields: service_type set to image_profile, plus file. Node.js 20 provides FormData, Blob and fetch, so this single-file example needs no multipart package. Pass the form as the body and let the runtime generate its boundary and Content-Type; setting a bare multipart/form-data header yourself would omit the matching boundary.
Save this as analyze-image.mjs and run it on your server with a local image path. Supply the key through a protected environment variable. The example intentionally does not print the key or send it to a log.
// Node.js 20+, run on a server with an API key in the environment.
import { readFile, stat } from 'node:fs/promises';
import { basename, extname } from 'node:path';
const types = new Map([
['.jpg', 'image/jpeg'], ['.jpeg', 'image/jpeg'],
['.png', 'image/png'], ['.webp', 'image/webp'],
['.gif', 'image/gif'],
]);
const imagePath = process.argv[2];
const apiKey = process.env.AVATARLOOKUP_API_KEY;
if (!imagePath || !apiKey) {
throw new Error('Provide an image path and AVATARLOOKUP_API_KEY');
}
const mime = types.get(extname(imagePath).toLowerCase());
if (!mime) throw new Error('Use a JPEG, PNG, WebP, or GIF image');
const details = await stat(imagePath);
if (!details.isFile() || details.size === 0 || details.size > 2 * 1024 * 1024) {
throw new Error('Provide one nonempty image file no larger than 2 MB');
}
const bytes = await readFile(imagePath);
const form = new FormData();
form.set('service_type', 'image_profile');
form.set('file', new Blob([bytes], { type: mime }), basename(imagePath));
let response;
try {
response = await fetch('https://api.avatarlookup.com/api/v1/image/profile', {
method: 'POST',
headers: { 'X-API-Key': apiKey },
body: form,
});
} catch (error) {
throw new Error(`Network request failed: ${error.message}`);
}
const text = await response.text();
let payload;
try {
payload = JSON.parse(text);
} catch {
throw new Error(`Expected JSON; HTTP ${response.status}`);
}
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${payload.msg ?? 'request failed'}`);
}
if (payload.code !== 0 || !payload.data) {
throw new Error(`Check not completed: ${payload.msg ?? 'unknown result'}`);
}
console.log({
imageFingerprint: payload.data.identifier,
category: payload.data.category,
result: payload.data,
});
The example reads the entire file into memory after checking its size. If you adapt it to receive files from other users, add your normal request authorization, file-content validation and storage controls before invoking the analysis API. Keep the service endpoint and field names aligned with the current Image profile product page and API reference.
Separate transport, HTTP and application outcomes
There are several ways a request can fail, and each calls for a different operator response. A thrown fetch error means the client did not obtain an HTTP response; check network connectivity and your server logs. A non-2xx HTTP response means the server replied with an error, so record the status and the safe message, without logging the key or the full uploaded image. A successful HTTP status with a nonzero application code is still not a completed analysis. Only continue when the response is valid JSON, the HTTP status is successful, code is zero and data is present.
The returned data object can include a picture category and optional appearance estimates. The example prints category, the content fingerprint from identifier, and the complete result object for local inspection. An absent age or an unknown attribute is not a reason to invent a value or mark the image as a failed upload.
A completed category describes the image, not the account that supplied it. A person-like category is not identity verification; an appearance estimate is not a confirmed age or gender. For a contact-card review, attach the result to your own source record and allow a human to decide whether the picture is suitable for that use. This keeps an algorithmic observation separate from your business decision.
Try the integration with a controlled image
Begin with one image you are allowed to process, below the size limit and in a supported format. Confirm that the response passes all three result gates, then inspect the category and any optional fields actually returned.
Once the single-file path works, map the returned content fingerprint and selected analysis fields into your application record alongside the source image reference and check time. The API analyzes the uploaded picture; your application remains responsible for linking the observation to the right record and deciding how to use it. See the image profile service for the current contract before deploying the integration.
Related pages
Learn More
Choose the product information that fits the next step in your workflow.