Skip to content

Calling the API

Open .md

The rest of these docs show requests with curl. This page shows the same pattern in three languages: one helper that every call goes through, a read, and a write.

The helper does five things on every request:

  1. Sends the key as Authorization: Bearer YOUR_API_KEY.
  2. Sends JSON with Content-Type: application/json.
  3. Asks for JSON with Accept: application/json, so even a wrong path answers JSON.
  4. Asks for exact amounts with X-Yoprint-Decimal-Format: exact-v1, so values that cannot be represented as exact JSON numbers (such as fractional thousandths or large integers) return as strings (see Money and quantities).
  5. Treats an answer as failed when its status is 4xx or 5xx or its body has "success": false, and raises an error that carries the status and the body (see Errors).

The helper only parses JSON. For a file download, such as a CSV export or a PDF, read the response body as bytes instead.

Keep the key in an environment variable, here YOPRINT_API_KEY, never in the code.

Node.js 18 or later, or any runtime with fetch.

const BASE = 'https://secure.yoprint.com';
const TEAM = 'your-team';
export async function yoprint(method, path, body) {
const response = await fetch(`${BASE}${path.replace('{team_slug}', TEAM)}`, {
method,
headers: {
Authorization: `Bearer ${process.env.YOPRINT_API_KEY}`,
'Content-Type': 'application/json',
Accept: 'application/json',
'X-Yoprint-Decimal-Format': 'exact-v1',
},
body: body === undefined ? undefined : JSON.stringify(body),
});
if (response.status === 204) return null; // one delete answers with no body
const json = await response.json().catch(() => null);
if (!response.ok || json === null || json.success === false) {
const error = new Error(`YoPrint ${response.status}: ${json?.error?.code ?? json?.message ?? 'error'}`);
error.status = response.status;
error.body = json;
throw error;
}
return json;
}
GET List customers /v1/api/store/{team_slug}/customer
const page = await yoprint('GET', '/v1/api/store/{team_slug}/customer?page=1');
for (const customer of page.data) {
console.log(customer.id, customer.name);
}
console.log(`Page ${page.pagination.currentPage} of ${page.pagination.totalPages}`);
POST Create a customer /v1/api/store/{team_slug}/customer

A create isn’t safe to repeat: sending it twice makes two customers. The examples don’t retry it. See Retries and duplicates.

try {
const created = await yoprint('POST', '/v1/api/store/{team_slug}/customer', {
customer: { name: 'Northwind Apparel', tax_exempt: false },
contact: [
{ first_name: 'Alex', last_name: 'Rivera', email: 'alex@example.com', send_invoice: true, is_primary: true },
],
});
console.log('Created', created.data.id);
} catch (error) {
if (error.status === 422) {
console.error('Fix these fields:', error.body?.error?.fields);
} else {
throw error;
}
}

A few operations answer 200 without doing what you asked. Publishing an order is the main one: check data.valid.

const published = await yoprint('PATCH', '/v2/api/store/{team_slug}/v2/sales_order/V4keXwwxMQyXw7/publish');
if (!published.data.valid) {
throw new Error(`Not published: ${JSON.stringify(published.data.validation)}`);
}

See Errors for the others.