Skip to main content
On the edge gateway every API runs as its own Worker, generated from the Target form. When the form is not enough, for example to transform the origin’s response, call several upstreams per request, or compute usage from the response body in a way no source expresses, take over the code.
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 generated index.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 file jojapi.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

Call ctx.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-proxy header holding the proxy URL, which ctx.proxy sets from a target’s proxy field.
  • Exceptions in your handler answer the consumer with a gateway 500 and are written to the API’s log.
  • Returned x-jojapi-* headers are reserved and removed; the gateway adds the usage and quota headers itself.