Custom code is available for APIs served by the edge gateway (the Target tab shows an Edge gateway badge).
Ejecting from the form
On the Target tab, View code shows the generatedindex.mjs; Edit this code turns it into your file. From then on:
- your code runs as the API’s Worker; the targets form is kept for reference and no longer applies,
- variables still reach the Worker as
env.NAME(secrets included), - every save deploys the new code within seconds,
- Back to the form regenerates the Worker from the targets and discards your code.
The SDK
The filejojapi.mjs is uploaded next to your code. It handles the platform contract (authentication, metering, logging) so your code only deals with the request.
defineApi accepts three keys, alone or combined:
ctx
env holds your variables (env.UPSTREAM_API_KEY) as strings.
Reporting usage
Callctx.usage() with the billable object slug as key. Rules of the platform still apply: on a fixed cost the reported amount can lower the bill but never exceed the declared amount; a 5xx response bills 0 on every object; values must be non-negative integers. An endpoint whose objects are not reported bills its fixed amounts, or 0 for metered objects.
Streaming responses (text/event-stream) pass through untouched; report usage in-band with a data: {"x-jojapi-credits-used": n} message as described in Adjusting usage.
Limits and behaviour
- Up to 30 seconds of CPU time and 50 outgoing requests per invocation; an upstream call has at most 90 seconds.
- IP addresses, non-standard ports and proxies are reached through the platform’s egress relay automatically; a proxy is chosen with the
x-jojapi-relay-proxyheader holding the proxy URL, whichctx.proxysets from a target’sproxyfield. - Exceptions in your handler answer the consumer with a gateway
500and are written to the API’s log. - Returned
x-jojapi-*headers are reserved and removed; the gateway adds the usage and quota headers itself.
