functions/ in your project with an index.ts that exports a named handler:
functions/block-po-boxes/index.ts
handler. A default export is ignored.
What you receive
Both are standard
Request / Response objects with one improvement: their bodies can be read more than once. await req.json() in one function and again in the next is fine. Two things to remember:
- A
GETrequest has no body, soreq.json()throws on it. Checkreq.methodfirst. formData()is not supported; readtext()and parse it yourself.
req is the absolute platform URL, for example https://acme.vtexcommercestable.com.br/api/checkout/pub/orderForm/<id>/items.
What you can return
Anything else (
null, an object, a string) is an error: Custom function must return a Request, a Response, or undefined.
Rewriting a request
Enriching a response
The checkout parses the platform’s response into the session. Fields you add underextensions surface as session.extensions in useCheckoutSession, which is how a function hands data to a component:
Rejecting an action with a message
A request function that returns aResponse stops the call. To show the shopper a message instead of a generic error, answer with a 4xx status and the x-ollie-shop-custom-message header; the checkout displays that text:
Several functions on one call
- All matching request functions run first, then the platform is called once, then all matching response functions run.
- Within each kind, functions run in priority order: lowest number first, ties by creation date. Priority never reorders a request function after a response function.
- Each function receives the output of the previous one: a request rewritten by priority 0 is what priority 1 sees.
- A request function that returns a
Responseends the chain: the remaining request functions, the platform call and the response functions are skipped.
Errors
A function fails when it throws, returns an invalid value, exceeds its time limit, or its trigger expression cannot be evaluated. What happens next is the function’s Error Behavior:
Use Throw only for rules that must block the action. For anything else, catch your own errors and use Skip, so a third-party outage cannot break checkout.
Environment variables and secrets
The store’s Environment Variables are available asprocess.env.MY_KEY inside the handler. They are injected per call, so read them in the handler, not at the top of the module:
Logging
console.log, console.warn and console.error are captured as structured logs with the function id and store id. Function logs are available on request from Ollie support. Keep lines short and avoid logging full payloads.
Runtime and limits
Types
The platform package that wraps your handler is not published, so declare the context type yourself or copy this one:functions/types.ts
Related
- Triggers: choosing which calls run the function
- Session storage and cookies: state across calls
- function dev: running it locally
- Deploying: the record, the code and activation