Example
Call the price function from an API
Write a function once, call it everywhere. A quote API on the checkout service takes a subtotal and a discount, calls Price with tax with the shop’s tax rate and answers with the total. A wrong request gets a 400 instead of an error.
You will build
- The POST /api/v1/quotes API on Checkout Service
- Endpoint logic that calls Price with tax
- Where used, linking the function to its callers
System map
- StorefrontCheckout ServicePOST /api/v1/quotes
Part 1: Declare the quote API
- 1.
Open Checkout Service → APIs → Add an API. Provided, POST,/api/v1/quotes, summaryPrice a cart, lifecycleTo do. - 2.
Request body: subtotal: number discount?: number
- 3.
200 body total: number. Click +400, bodyerror: string. Click Create endpoint.
Part 2: Build the endpoint logic
- 1.
On the POST /api/v1/quotesrow, click + Logic. - 2.
Add Break Object fed from On Request body:subtotal: number discount?: number
- 3.
Add Default Value: valuefromdiscount,fallback=0. A missing discount counts as none. - 4.
Add Validate Range on subtotalwith Field namesubtotal, Min0, and clear Max. Then a BranchSubtotal valid?. - 5.
From False: Return Response 400, body Make Objecterror: stringfrom the validationmessage. - 6.
From True: add Call Function. In Details, pick Price with tax. Its connection points are the function’s inputs and outputs. Connectsubtotal,taxRate=0.12, anddiscountfrom the Default Valueresult. - 7.
Then Return Response 200, body Make Objecttotal: numberfrom the call’stotal.
Check:
Part 3: Test it and find the callers
- 1.
Click Test. Body { "subtotal": 100 }answers 200 withtotal112. Body{ "subtotal": -5 }answers 400. - 2.
Open Price with taxand its Where used tab: the quote logic is listed under Called by. Change the function, and every caller gets the change.
