Request and response
An endpoint with its request on paper and its response on a dark pane, with tabs for a success and a validation error. Sits side by side when there's room.
/v2/invoicesCreate an invoice from a signed-off job sheet.Request
Authorization: Bearer $HALDEN_KEY
Content-Type: application/json
{
"job": "JS-2207",
"due_days": 30,
"send": true
}
Response
{
"id": "inv_0418",
"number": "INV-0418",
"status": "sent",
"total": 2140.00,
"currency": "USD",
"pdf": "/v2/invoices/inv_0418.pdf"
}
{
"error": "job_not_signed_off",
"message": "JS-2207 has no sign-off yet",
"field": "job"
}
201 Created · 184 ms422 Unprocessable · 61 ms
The code
Plain HTML and CSS on your kit's variables. Put it next to tokens/variables.css from your kit's files and it draws in your look.
<div class="ukc-code-api-pair">
<div class="api">
<div class="ep"><span class="verb">POST</span><code class="path">/v2/invoices</code><span class="what">Create an invoice from a signed-off job sheet.</span></div>
<div class="pair">
<section class="req" aria-labelledby="ukc-code-api-pair-req">
<h4 class="lb" id="ukc-code-api-pair-req">Request</h4>
<pre class="src"><code><span class="l"><span class="hk">Authorization:</span> Bearer $HALDEN_KEY</span>
<span class="l"><span class="hk">Content-Type:</span> application/json</span>
<span class="l"> </span>
<span class="l">{</span>
<span class="l"> "job": <span class="s">"JS-2207"</span>,</span>
<span class="l"> "due_days": <span class="n">30</span>,</span>
<span class="l"> "send": <span class="k">true</span></span>
<span class="l">}</span></code></pre>
</section>
<section class="res" aria-labelledby="ukc-code-api-pair-res">
<input class="r r-ok" type="radio" name="ukc-code-api-pair-status" id="ukc-code-api-pair-201" checked>
<input class="r r-bad" type="radio" name="ukc-code-api-pair-status" id="ukc-code-api-pair-422">
<div class="rh"><h4 class="lb" id="ukc-code-api-pair-res">Response</h4><div class="codes"><label class="t-ok" for="ukc-code-api-pair-201"><span class="dot"></span>201</label><label class="t-bad" for="ukc-code-api-pair-422"><span class="dot"></span>422</label></div></div>
<pre class="src p-ok"><code><span class="l">{</span>
<span class="l"> "id": <span class="s">"inv_0418"</span>,</span>
<span class="l"> "number": <span class="s">"INV-0418"</span>,</span>
<span class="l"> "status": <span class="s">"sent"</span>,</span>
<span class="l"> "total": <span class="n">2140.00</span>,</span>
<span class="l"> "currency": <span class="s">"USD"</span>,</span>
<span class="l"> "pdf": <span class="s">"/v2/invoices/inv_0418.pdf"</span></span>
<span class="l">}</span></code></pre>
<pre class="src p-bad"><code><span class="l">{</span>
<span class="l"> "error": <span class="s">"job_not_signed_off"</span>,</span>
<span class="l"> "message": <span class="s">"JS-2207 has no sign-off yet"</span>,</span>
<span class="l"> "field": <span class="s">"job"</span></span>
<span class="l">}</span></code></pre>
<p class="ft"><span class="p-ok">201 Created · 184 ms</span><span class="p-bad">422 Unprocessable · 61 ms</span></p>
</section>
</div>
</div>
</div>
.ukc-code-api-pair { position: relative; font-family: var(--uk-font-body, system-ui, sans-serif); color: var(--uk-ink); font-size: 15px; line-height: 1.5;
--tint: color-mix(in srgb, var(--uk-accent) 12%, var(--uk-paper));
--tint-strong: color-mix(in srgb, var(--uk-accent) 22%, var(--uk-paper));
--accent-deep: color-mix(in srgb, var(--uk-accent) 72%, black);
--ink-soft: color-mix(in srgb, var(--uk-ink) 8%, var(--uk-paper));
--line-strong: color-mix(in srgb, var(--uk-ink) 32%, var(--uk-paper));
--danger-tint: color-mix(in srgb, var(--uk-danger) 12%, var(--uk-paper));
--high-tint: color-mix(in srgb, var(--uk-highlight) 28%, var(--uk-paper)); }
.ukc-code-api-pair *, .ukc-code-api-pair *::before, .ukc-code-api-pair *::after { box-sizing: border-box; }
.ukc-code-api-pair :where(h1, h2, h3, h4, p, ul, ol, dl, dd, figure, blockquote) { margin: 0; }
.ukc-code-api-pair :where(ul, ol) { padding: 0; list-style: none; }
.ukc-code-api-pair :where(button, input, select, textarea) { font: inherit; color: inherit; }
.ukc-code-api-pair :where(a) { color: inherit; }
.ukc-code-api-pair svg { flex: none; }
.ukc-code-api-pair .r { position: absolute; opacity: 0; pointer-events: none; }
.ukc-code-api-pair { container-type: inline-size; inline-size: 100%; }
.ukc-code-api-pair .api { max-width: 720px; border: var(--uk-border-width) solid var(--uk-line); border-radius: var(--uk-radius-lg); overflow: hidden; background: var(--uk-surface); box-shadow: var(--uk-card-shadow); }
.ukc-code-api-pair .ep { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 10px; padding: 12px 16px; border-bottom: 1px solid var(--uk-line); }
.ukc-code-api-pair .verb { padding: 1px 8px; border-radius: var(--uk-radius-sm); background: var(--uk-accent); color: var(--uk-accent-ink); font: 700 11.5px/1.6 var(--uk-font-mono, ui-monospace, monospace); letter-spacing: 0.04em; }
.ukc-code-api-pair .path { font: 600 14px/1.5 var(--uk-font-mono, ui-monospace, monospace); }
.ukc-code-api-pair .what { flex-basis: 100%; color: var(--uk-muted); font-size: 13.5px; }
.ukc-code-api-pair .pair { display: grid; }
.ukc-code-api-pair .lb { font-size: 11px; font-weight: 600; letter-spacing: 0.08em; text-transform: uppercase; }
.ukc-code-api-pair .req .lb { padding: 12px 16px 0; color: var(--uk-muted); }
.ukc-code-api-pair .src { margin: 0; overflow-x: auto; font: 12.5px/1.7 var(--uk-font-mono, ui-monospace, monospace); tab-size: 2; }
.ukc-code-api-pair .src code { display: grid; width: max-content; min-width: 100%; font: inherit; }
.ukc-code-api-pair .src .l { min-height: 1.7em; padding: 0 16px; white-space: pre; }
.ukc-code-api-pair .src { padding: 8px 0 14px; }
.ukc-code-api-pair .req .hk { color: var(--uk-muted); }
.ukc-code-api-pair .req .k { color: color-mix(in srgb, var(--uk-accent) 60%, var(--uk-ink)); }
.ukc-code-api-pair .req .s { color: color-mix(in srgb, var(--accent-deep) 60%, var(--uk-ink)); background: var(--tint); }
.ukc-code-api-pair .req .n { color: color-mix(in srgb, var(--accent-deep) 60%, var(--uk-ink)); }
.ukc-code-api-pair .req .c { color: var(--uk-muted); font-style: italic; }
.ukc-code-api-pair .req .f { font-weight: 600; }
.ukc-code-api-pair .req .s { background: none; }
.ukc-code-api-pair .res { position: relative; display: flex; flex-direction: column; background: color-mix(in srgb, var(--uk-ink) 12%, black); color: color-mix(in srgb, var(--uk-paper) 15%, white); }
.ukc-code-api-pair .rh { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 8px 10px 0 16px; }
.ukc-code-api-pair .res .lb { color: color-mix(in srgb, white 55%, transparent); }
.ukc-code-api-pair .codes { display: flex; gap: 2px; }
.ukc-code-api-pair .codes label { display: inline-flex; align-items: center; gap: 6px; padding: 3px 9px; border-radius: var(--uk-radius-sm); color: color-mix(in srgb, white 60%, transparent); font: 600 12px/1.5 var(--uk-font-mono, ui-monospace, monospace); cursor: pointer; transition: background-color var(--uk-motion-duration) var(--uk-motion-ease), color var(--uk-motion-duration) var(--uk-motion-ease); }
.ukc-code-api-pair .codes label:hover { color: white; }
.ukc-code-api-pair .codes .dot { width: 7px; height: 7px; border-radius: var(--uk-radius-full); background: color-mix(in srgb, white 70%, transparent); }
.ukc-code-api-pair .codes .t-bad .dot { background: color-mix(in srgb, var(--uk-danger) 55%, white); }
.ukc-code-api-pair .r-ok:checked ~ .rh .t-ok, .ukc-code-api-pair .r-bad:checked ~ .rh .t-bad { background: color-mix(in srgb, white 12%, transparent); color: white; }
.ukc-code-api-pair .r-ok:focus-visible ~ .rh .t-ok, .ukc-code-api-pair .r-bad:focus-visible ~ .rh .t-bad { outline: 2px solid color-mix(in srgb, var(--uk-accent) 55%, white); outline-offset: 1px; }
.ukc-code-api-pair .res .src { flex: 1; }
.ukc-code-api-pair .res .p-bad { display: none; }
.ukc-code-api-pair .r-bad:checked ~ .p-ok, .ukc-code-api-pair .r-bad:checked ~ .ft .p-ok { display: none; }
.ukc-code-api-pair .r-bad:checked ~ .p-bad { display: block; }
.ukc-code-api-pair .r-bad:checked ~ .ft .p-bad { display: inline; }
.ukc-code-api-pair .res .k { color: color-mix(in srgb, var(--uk-accent) 55%, white); }
.ukc-code-api-pair .res .s, .ukc-code-api-pair .res .n { color: color-mix(in srgb, var(--uk-highlight) 80%, white); }
.ukc-code-api-pair .res .c { color: color-mix(in srgb, white 50%, transparent); font-style: italic; }
.ukc-code-api-pair .res .f { color: white; font-weight: 600; }
.ukc-code-api-pair .ft { padding: 7px 16px; border-top: 1px solid color-mix(in srgb, white 12%, transparent); color: color-mix(in srgb, white 55%, transparent); font: 11.5px/1.5 var(--uk-font-mono, ui-monospace, monospace); }
@container (min-width: 620px) { .ukc-code-api-pair .pair { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); } }
More code blocks
All 24- job-sheet.json1.2 kB
{ 7 keys }
"id": "JS-2207","site": "14 Alder St","signed_off": true,"crew": [ 2 items ],
"Ines Ortega","Dana Whitfield"],"hours": { 3 keys },
"labor": 22.5,"travel": 1.5,"overtime": 0},"materials": [ 2 items ],
{ 3 keys },
"sku": "OAK-WT-30","qty": 1,"unit_cost": 41800},{ 3 keys }
"sku": "2x6-PT-12","qty": 40,"unit_cost": 1145}],"invoice": null}Path
$.materials[1].qty Step 3Handle the webhook
export async function POST(req: Request) { const raw = await req.text(); const event = verify(raw, req.headers); (note 1) if (event.type === "invoice.paid") { (note 2) await markPaid(event.invoice); } return new Response(null, { status: 204 }); (note 3) }Note 1:
verifythrows if the signature or timestamp is off, so a forged call never reaches your code.Note 2: Check the type first. New event types get added, so skip the ones you don't handle.
Note 3: Answer within 10 seconds. Slow or failed deliveries are retried for three days.
TypeError: Cannot read properties of undefined (reading 'rate')
lineTotalsrc/totals.ts:15:23
export function lineTotal(l: Line) { const price = PRICES[l.sku]; return l.qty * price.rate; ^^^^^ price is undefined }buildInvoicesrc/invoices.ts:42:18
for (const line of sheet.lines) { totals.push(lineTotal(line)); }POSTapp/api/invoices/route.ts:18:9
const sheet = await getSheet(body.job); const draft = buildInvoice(sheet);3 frames in node_modules
- handle @halden/sdk/dist/http.js:88:11
- dispatch router-lite/index.js:41:7
- run router-lite/index.js:12:3
- .env.local4 of 5 set
- # Halden, test mode
-
HALDEN_API_KEY=hidden, ends in 4f9chld_test_8Kq2Vn5x4f9c HALDEN_ENV=test-
DB_PASSWORD=hiddenlocal-dev-only - # Webhooks
HALDEN_WEBHOOK_SECRET=Empty Webhook checks fail until this is set. Copy it from Settings, then Webhooks.
- unpaid.sql
-- Unpaid and past due, oldest first SELECT j.code, c.name, i.total, current_date - i.due_on AS late FROM invoices i JOIN jobs j ON j.id = i.job_id JOIN clients c ON c.id = j.client_id WHERE i.paid_at IS NULL AND i.due_on < current_date ORDER BY late DESC;4 rows in 18 msExport CSV
code name total late JS-2216 Pier Ln Holdings 6,900.00 21 JS-2207 Reyes Framing 2,140.00 9 JS-2219 Mill Lane Lofts 1,212.50 4 JS-2223 O. Whitaker 480.00 1