Skip to content
Car Image API
Documentation

Car images in React and Next.js

A copy-paste Server Component: a signed URL created on your server, served to visitors from our CDN.

Your API key never reaches the browser. Code that runs on your server, a Server Component, a Route Handler or a Server Action, creates signed delivery URLs with POST /api/v1/image-urls, each counting one vehicle image of your plan's month when it is created, and the browser loads them with a plain <img>, free until they expire. The examples use the Next.js App Router and the zero-dependency TypeScript SDK; any React framework that renders on a server follows the same steps.

The short version

One Server Component and one page setting. The component creates a signed URL that renews itself, and the page renders once every 7 days instead of on every visit. Each image then counts once a month as a vehicle image, however many people see it, and visitors load it from our CDN. Add CAR_IMAGE_API_KEY to .env.local, run npm install @meterapp/car-image-sdk and paste this:

components/car-image.tsx and app/page.tsx
// components/car-image.tsx: a Server Component, so the key never reaches the browser.
import { CarImageClient } from "@meterapp/car-image-sdk";

const client = new CarImageClient({ apiKey: process.env.CAR_IMAGE_API_KEY });

export async function CarImage({ alt }: { alt: string }) {
  // 1 credit now; the browser then loads it from our CDN for free for 7 days.
  // renew: a cached page that outlives that keeps working, 1 credit per further week shown.
  const { data } = await client.createImageUrls(
    [{ make: "porsche", model: "911", year: 2024, view: "front-3-4", color: "red", width: 600, format: "auto" }],
    { ttlSeconds: 604800, renew: true },
  );
  return <img src={data[0].url} alt={alt} width={600} height={600} loading="lazy" />;
}

// app/page.tsx: render once every 7 days, not on every visit.
import { CarImage } from "@/components/car-image";

export const revalidate = 604800;

export default function Page() {
  return <CarImage alt="2024 Porsche 911" />;
}

The steps below explain each part: naming the car by its catalog id, creating a whole gallery in one call, what to show on a 402, and caching pages that are built once or rendered per request.

Already have a signed URL?

In a Vite app, a Client Component or any other React page, pass the URL your backend returned to <img src={deliveryUrl}>. Start with the short HTML and React examples; the steps below show how a Next.js server creates and caches those URLs.

1. Keep the key on the server

Keep the key in CAR_IMAGE_API_KEY: in your host's environment settings for deployments, and for development in .env.local, kept out of version control. Never give it the NEXT_PUBLIC_ prefix: Next.js inlines those variables into the JavaScript it sends to every browser. Start each module that reads the key with import "server-only", so a Client Component that imports it fails the build instead of shipping the key.

bash
npm install @meterapp/car-image-sdk
.env.local
CAR_IMAGE_API_KEY=cimg_your_key_here

2. Look the vehicle up once, render it by id

Name the car by its stable vehicle id (veh_…), not by a make and model typed from memory: the catalog files cars under its own names, so a name can answer 404 for a car it carries. Resolve the words or decode the VIN when a record is created, store the id with it (ids never change), and render by id from then on. Both lookups are free.

lib/vehicles.ts
import "server-only";
import { CarImageClient } from "@meterapp/car-image-sdk";

const client = new CarImageClient({ apiKey: process.env.CAR_IMAGE_API_KEY });

/** "2018 Mazda MX-5 Miata" to "veh_…", or null when a person should choose. Free. */
export async function vehicleIdFromText(text: string): Promise<string | null> {
  const { data } = await client.resolve(text);
  const askedYear = data.extracted?.year;
  if (data.confidence === "low" || (askedYear && askedYear !== data.params.year)) {
    return null; // show data.candidates and let a person pick
  }
  return data.params.vehicle_id;
}

/** A full or partial VIN to "veh_…", or null when it maps to no catalog vehicle. Free. */
export async function vehicleIdFromVin(vin: string): Promise<string | null> {
  const { data } = await client.decodeVin(vin);
  return data.vehicle?.id ?? null;
}

/** "2024 Porsche 911" for alt text and headings: GET /api/v1/vehicles/{id}, public and free. */
export async function vehicleName(id: string): Promise<string> {
  const { data } = await client.vehicle(id);
  return `${data.year} ${data.make.name} ${data.model.name}`;
}

medium confidence is an interpretation, such as a badge, trim words or a model name several makes share: render it, and say which vehicle you chose. A phrase that names nothing in the catalog answers 404, which the SDK throws as a CarImageError. A VIN can decode without a catalog match; ask for the make, model and year then. To map a table you already have, client.checkVehicles answers every row with the id to store, also for free. See resolve, backfilling a catalog and VIN decoding.

3. Create signed URLs on the server

One call creates a batch of URLs, one vehicle image each (nothing more for an image already counted this month), and returns them in the order you asked. Without ttlSeconds a URL lives 3,600 seconds; the example asks for the longest, 7 days, so a cached page can hold it. The SDK sends an Idempotency-Key with every such call, so its own retry after a dropped connection replays the first answer instead of creating a second batch; pass idempotencyKey to extend that across processes, such as a job queue that can deliver the same task twice.

lib/car-images.ts
import "server-only";
import { CarImageClient, isCarImageError, type ImageParams } from "@meterapp/car-image-sdk";

const client = new CarImageClient({ apiKey: process.env.CAR_IMAGE_API_KEY });

/** 7 days, the longest a signed URL lives. */
export const IMAGE_URL_TTL_SECONDS = 604800;

/**
 * Signed URLs for a batch of images, in one call: each counts one vehicle image of the month
 * now; loading them is free until they expire. Null when the plan's allowance is used up.
 */
export async function signedCarImages(images: ImageParams[]): Promise<string[] | null> {
  try {
    const { data } = await client.createImageUrls(images, { ttlSeconds: IMAGE_URL_TTL_SECONDS });
    return data.map((item) => item.url);
  } catch (error) {
    // 402: the plan's vehicle images for the month are used up. A retry cannot
    // fix it, and nothing was charged: show a placeholder and tell a person.
    if (isCarImageError(error) && error.status === 402) {
      console.error(`Car Image API: ${error.detail} (request ${error.requestId})`);
      return null;
    }
    throw error; // anything else fails this render; an ISR page keeps its last good version
  }
}

The body the SDK sends is the REST contract on Signed URLs; any HTTP client can make the same call.

4. Render it with a plain img

A Server Component awaits the URL and renders an ordinary image element. Ask for the box you will show (width and height, with trim so the car fills it) and format: "auto", which serves WebP or PNG to each browser, and put the same width and height on the element so the page does not shift while it loads. When the plan's allowance is used up, the component shows a placeholder.

components/car-image.tsx
import type { View } from "@meterapp/car-image-sdk";
import { signedCarImages } from "@/lib/car-images";

export async function CarImage({
  vehicle,
  view = "front-3-4",
  alt,
  width = 600,
  height = 400,
}: {
  vehicle: string; // a veh_… id
  view?: View;
  alt: string;
  width?: number;
  height?: number;
}) {
  const urls = await signedCarImages([{ vehicle, view, width, height, trim: true, format: "auto" }]);
  if (!urls) {
    return <div role="img" aria-label={alt} style={{ width, height, background: "#f4f4f5" }} />;
  }
  // eslint-disable-next-line @next/next/no-img-element -- the API already sized and encoded it
  return <img src={urls[0]} alt={alt} width={width} height={height} loading="lazy" decoding="async" />;
}

A 402 means the plan's vehicle images for the month are used up, and nothing was charged. Retrying cannot fix it, so the placeholder stays until the page next renders: the allowance resets on the 1st (UTC; Free's does not), or change the plan in the dashboard. Every case is in the error reference.

5. Cache no longer than the URL lives

A signed URL answers 403 once it expires, so nothing that stores your HTML may keep it longer: a prerendered page, an ISR page or a CDN in front of your site. Every render creates new URLs, so the aim is to render rarely and never outlive the URLs.

  • Revalidate well inside the TTL. With URLs that live 7 days, regenerate pages daily. Next.js answers the first request after the revalidate window with the stale page while it regenerates, so a page nobody opens for longer than the TTL shows that first visitor an expired URL.
  • Renewing URLs for pages built once. For a static export, rarely visited pages or HTML a CDN keeps, create them with renew: true. The URL keeps loading after it expires: the first load in each new window of the TTL renews it (an image counts once in the month it lands in; windows nobody opens count nothing), for 365 days after creation by default. Rebuild before then. See auto-renewing URLs.
  • Never create a URL per request. A page rendered on every visit that creates makes a request per image per page view. If it must be dynamic, keep each URL with its expires_at (in your database, a key-value store or the Next.js cache) and reuse it until shortly before it expires.
app/vehicles/[id]/page.tsx
import { CarImage } from "@/components/car-image";
import { vehicleName } from "@/lib/vehicles";

// Regenerate at most once a day, well inside the 7 days the URLs live.
export const revalidate = 86400;

// No paths at build time: each page is rendered on its first visit, then cached.
// Without generateStaticParams the route renders, and creates URLs, on every request.
export async function generateStaticParams() {
  return [];
}

export default async function VehiclePage({ params }: { params: Promise<{ id: string }> }) {
  const { id } = await params; // a veh_… id you stored
  const name = await vehicleName(id);
  return (
    <main>
      <h1>{name}</h1>
      <CarImage vehicle={id} view="side" alt={`${name}, side view`} />
    </main>
  );
}
lib/car-images.ts, for renewing URLs
// For HTML that outlives the TTL: the URLs keep loading after they expire.
const { data } = await client.createImageUrls(images, {
  ttlSeconds: IMAGE_URL_TTL_SECONDS,
  renew: true, // renews in each further window in which a URL is opened
});

With Cache Components enabled, "use cache" and cacheLife replace revalidate; the rule is the same.

6. Client Components

A Client Component never holds the key. When a Server Component renders it, create the URLs there and pass them down as props: a view picker gets every view it offers from one call.

components/car-gallery.tsx
import type { View } from "@meterapp/car-image-sdk";
import { signedCarImages } from "@/lib/car-images";
import { ViewPicker } from "./view-picker";

const GALLERY_VIEWS: View[] = ["front-3-4", "side", "rear-3-4", "front"];

// A Server Component: one call creates every view (one vehicle image each); the picker only switches.
export async function CarGallery({ vehicle, alt }: { vehicle: string; alt: string }) {
  const urls = await signedCarImages(
    GALLERY_VIEWS.map((view) => ({ vehicle, view, width: 800, height: 500, trim: true, format: "auto" })),
  );
  if (!urls) return null;
  return <ViewPicker alt={alt} images={GALLERY_VIEWS.map((view, i) => ({ view, url: urls[i] }))} />;
}
components/view-picker.tsx
"use client";

import { useState } from "react";

export function ViewPicker({ alt, images }: { alt: string; images: { view: string; url: string }[] }) {
  const [current, setCurrent] = useState(images[0]);
  return (
    <figure>
      {/* eslint-disable-next-line @next/next/no-img-element -- a signed URL, sized by the API */}
      <img src={current.url} alt={`${alt}, ${current.view} view`} width={800} height={500} />
      {images.map((image) => (
        <button key={image.view} type="button" aria-pressed={image === current} onClick={() => setCurrent(image)}>
          {image.view}
        </button>
      ))}
    </figure>
  );
}

When the browser decides what to show, such as a paint picked in a configurator, let it call a Route Handler or Server Action of yours that creates them. Create URLs only for vehicles your product shows: anyone can call that endpoint, and the terms rule out proxying raw access to the API. Keep each URL with its expires_at and hand out the same one until shortly before it expires, or every click costs a request.