New Light partners with Amesto GlobalLight and Amesto Global Read the announcementRead more

API/Examples

Create a purchase request

Raise a purchase request, retry it safely, follow it through approval to its purchase order, then edit or cancel it.

Section
Examples
Reading
5 min read
On this page
  1. 01Before you start
  2. 02Create the request
  3. 03Retry safely
  4. 04Follow it through approval
  5. 05Edit a request
  6. 06Cancel a request

A purchase request asks for approval to spend with a vendor. Once it is approved, Light turns it into a purchase order with the same vendor, entity, currency and lines. This example raises a request from an intake or procurement tool, retries it safely, follows it through approval, then edits and cancels it.

Before you start

  • The vendor must already exist in Light, be active, and be assigned to the entity you raise the request in. Find it with List vendors, or add it with Create vendor. A request for a vendor not yet in Light can only be raised in the product.
  • Requests raised through the API always become purchase orders. Vendor card requests are raised in the product. The API can read and cancel them, but not edit them.
  • The credential needs purchase-requests:create to raise a request for anyone. purchase-requests/own:create covers only requests whose requestedBy is the credential's own user.

Create the request

Save the body as purchase-request.json. Replace the example IDs with your requester, entity, vendor, ledger account and tax code.

{
  "requestedBy": "6f1c2b9e-3d4a-4f7b-9a51-2c8e7d0b4a13",
  "companyEntityId": "dd991863-2e80-4a47-a917-a289bcba7e23",
  "description": "Standing desks for the London office",
  "currency": "GBP",
  "vendorId": "3c90c3cc-0d44-4b50-8888-8dd25736052a",
  "lines": [
    {
      "description": "Standing desk",
      "quantity": 4,
      "unitPrice": 45000,
      "ledgerAccountId": "eb7f9d33-34d2-4d1b-a787-d388619c5652",
      "taxCodeId": "08b92767-8745-4c87-9ea9-f5b60aad5d89"
    },
    {
      "description": "Delivery and assembly",
      "quantity": 1,
      "unitPrice": 12000
    }
  ]
}
  • unitPrice is in minor units, so 45000 is £450.00. You don't send an amount: Light sums the lines, rounding each line to whole minor units, so this request's amount is 192,000 (£1,920.00). Approval routes on that sum.
  • quantity takes up to 4 decimal places. Send plain numbers such as 120, not 1.2E+2.
  • A request holds at most 500 lines, the most a purchase order can hold.
  • ledgerAccountId and taxCodeId are optional on each line, and must belong to the request's entity.
  • customProperties, on the request and on each line, take group and value IDs. Required request-level groups must be sent on create. See Pagination, filtering and errors for how custom property writes work.

Send it with Create purchase request and an X-Idempotency-Key:

const fs = require('node:fs');
const BASE_URL = 'https://api.light.inc';

async function api(path, options = {}) {
  const res = await fetch(`${BASE_URL}${path}`, {
    ...options,
    headers: {
      Authorization: `Basic ${process.env.LIGHT_API_KEY}`,
      'Content-Type': 'application/json',
      ...options.headers,
    },
    signal: AbortSignal.timeout(30000),
  });
  const text = await res.text();
  if (!res.ok) throw new Error(`${res.status}: ${text}`);
  return JSON.parse(text);
}

// Generate the key once per request, store it with your work item, and reuse it on every retry.
async function createPurchaseRequest(idempotencyKey) {
  return api('/v1/purchase-requests', {
    method: 'POST',
    headers: { 'X-Idempotency-Key': idempotencyKey },
    body: fs.readFileSync('./purchase-request.json', 'utf8'),
  });
}
import json
import os
from pathlib import Path
import requests

BASE_URL = 'https://api.light.inc'

def api(method, path, body=None, key=None):
    headers = {'Authorization': f"Basic {os.environ['LIGHT_API_KEY']}"}
    if key is not None:
        headers['X-Idempotency-Key'] = key
    res = requests.request(method, BASE_URL + path, headers=headers, json=body, timeout=30)
    if not res.ok:
        raise RuntimeError(f'{res.status_code}: {res.text}')
    return res.json()

# Generate the key once per request, store it with your work item, and reuse it on every retry.
def create_purchase_request(idempotency_key):
    body = json.loads(Path('purchase-request.json').read_text())
    return api('POST', '/v1/purchase-requests', body=body, key=idempotency_key)

Store the returned id before doing anything else with it.

Retry safely

  • On a timeout or 5xx, retry with backoff, sending the same body with the same key. Within 24 hours, Light returns the request the first call created instead of creating a second one. The replay reads the request as it is now, so its status may have moved on since the first call.
  • A 400 means Light refused the request and saved nothing. The key is freed, so fix the body and send it again with the same key.
  • Without a key, a retry after a timeout can create a duplicate request. See Idempotency for backoff and the other responses.

Follow it through approval

The create response already carries the request's status:

Status Meaning
IN_REVIEW Waiting for the approvers in the company's purchase request approval workflow.
APPROVED Approved, and the purchase order is created. outcome.purchaseOrderId holds its ID.
REJECTED An approver rejected it. Editing it sends it back for approval.
FAILED Approved, but Light couldn't create the purchase order. failureContext says why. It can be canceled, not edited.
CANCELLED Canceled, together with its purchase order.
CANCEL_PENDING Only older requests can be in this status.

If the company hasn't published a purchase request approval workflow, there is no one to approve, so the create call approves the request and creates its purchase order straight away and returns APPROVED. If that purchase order can't be created, the create call fails and no request is saved.

The purchase order starts as a draft whose vendor, entity, currency, line quantities and prices are locked to what was approved. Read it with Get purchase order.

Nothing calls you when a request changes. Poll Get purchase request for one request, or sync many with List purchase requests filtered on updatedAt, for example filter=updatedAt:gte:2026-11-27T09:00:00Z with cursor=0. updatedAt is when a change started saving, so start each sync a few minutes before the last updatedAt you saw.

Edit a request

Update purchase request changes a request that is IN_REVIEW or REJECTED. Send only the fields you are changing. It needs purchase-requests:update, or purchase-requests/own:update for a request the credential created.

  • lines replaces every line, and the amount is summed again from the new lines. Without lines, the lines and amount stay as they are.
  • A new currency must come with lines priced in that currency, or the update is refused with PURCHASE_REQUEST_CURRENCY_CHANGE_WITHOUT_OUTCOME.
  • Changing requestedBy to another user needs purchase-requests:create.
  • Changing anything other than description (the lines, currency, vendor, entity, requester or custom properties) starts approval again. So does any edit to a REJECTED request.
  • Vendor card requests are refused with PURCHASE_REQUEST_VENDOR_CARD_UPDATE_NOT_SUPPORTED.

Cancel a request

Cancel purchase request cancels a request that is IN_REVIEW, FAILED or APPROVED, together with its purchase order, and needs purchase-requests:archive. It returns a 400 for a request that is rejected or already canceled, a vendor card request whose card was issued, and one whose purchase order can no longer be canceled.

Cancel takes no idempotency key. If a cancel times out, read the request before trying again: if its status is already CANCELLED, the first call went through.