Node.js client (SDK)
Use the API from Node.js with @cliquealo/api-client, which already paginates, respects limits, retries, and verifies notifications.
@cliquealo/api-client is the official API client for Node.js 20 or later. It has no dependencies, ships with
TypeScript types, and already handles what the other guides explain by hand: requesting inventory pages, waiting when
you hit a limit, retrying when the network fails, and verifying the signature of notifications.
Install
npm install @cliquealo/api-clientThe package is at npmjs.com/package/@cliquealo/api-client.
Create the client
import { Cliquealo } from "@cliquealo/api-client";
const cliente = new Cliquealo({ apiKey: process.env.CLIQUEALO_API_KEY! });Use it only on your server: the API key must never reach a browser or an app that can be downloaded.
Inventory
for await (const auto of cliente.inventario.listar({ marca: "toyota", lang: "en" })) {
console.log(auto.slug, auto.titulo, auto.precio);
}
const detalle = await cliente.inventario.obtener("toyota-corolla-2023");
const nuevo = await cliente.inventario.crear({
marca: "Toyota",
modelo: "Corolla",
anio: 2023,
precio_centavos: 35000000,
moneda: "MXN",
});
await cliente.inventario.actualizar(nuevo.slug, { precio_centavos: 34000000 });
await cliente.inventario.archivar(nuevo.slug);listar fetches cars 48 at a time and only requests the next page when you are about to use it. If you'd rather handle
pagination yourself, pagina({ limit, offset }) returns a single page.
For other platforms, plataformas(slug) tells you which ones a car is on, and registrarPlataforma(slug, { platform, status, external_url }) reports that you published it on one.
Catalog
const marcas = await cliente.catalogo.marcas();
const modelos = await cliente.catalogo.modelos(marcas[0].id);There are also colores(), entidades(), ciudades(entidad), carrocerias(), transmisiones(), and
combustibles().
Quotes
const productos = await cliente.cotizaciones.productos();
const cotizacion = await cliente.cotizaciones.crear({
publicacion_slug: "toyota-corolla-2023",
producto_credito_id: productos[0].id,
enganche_mxn: 70000,
plazo_meses: 48,
contacto_email: "cliente@correo.com",
modo: "email",
});With modo: "email" we send the PDF to the customer and you get the quote back. With modo: "pdf" you get
{ id, folio, pagoMensualMxn, pdf }, with the PDF ready for you to save or send yourself.
Signed notifications
import { verificarFirma } from "@cliquealo/api-client";
export async function POST(request: Request) {
const cuerpo = await request.text();
const firma = request.headers.get("x-cliquealo-signature");
if (!verificarFirma(cuerpo, firma, process.env.CLIQUEALO_SECRETO_AVISOS!)) {
return new Response(null, { status: 400 });
}
const aviso = JSON.parse(cuerpo);
await guardarAviso(aviso);
return new Response(null, { status: 200 });
}Verify against the body exactly as it arrived, before parsing it as JSON. leerAviso(cuerpo, firma, secreto) does both:
it verifies and returns the notification, or throws an error if the signature is invalid or expired. What each
notification contains is in Notifications to your system.
Errors, limits, and retries
- Limits. On a 429 the client waits for what
Retry-Aftersays and retries, up to 3 times. - Failures. Reads, updates, and
archivarare retried up to 3 times on 5xx or network errors, waiting 1, 2, and 4 seconds. - Creates and quotes. They're only retried on 429 or 503, so nothing gets created twice.
- Everything else throws
ErrorCliquealowithstatus,codigo,campo,scope,esperaSegundos, and the API's message.
import { ErrorCliquealo } from "@cliquealo/api-client";
try {
await cliente.inventario.crear(alta);
} catch (error) {
if (error instanceof ErrorCliquealo && error.codigo === "campo_invalido") {
console.log(error.campo, error.message);
}
}If you want to handle retries yourself, create the client with reintentos: 0.