Cloudflare Docs
Pages
Visit Pages on GitHub
Set theme to dark (⇧+D)

API reference

The following APIs are used within Functions.

​​ Methods

​​ onRequests

  • onRequest(contextEventContext) Response | Promise<Response>
    • This function will be invoked on all requests no matter the request method
  • onRequestGet(contextEventContext) Response | Promise<Response>
    • This function will be invoked on all GET requests
  • onRequestPost(contextEventContext) Response | Promise<Response>
    • This function will be invoked on all POST requests
  • onRequestPatch(contextEventContext) Response | Promise<Response>
    • This function will be invoked on all PATCH requests
  • onRequestPut(contextEventContext) Response | Promise<Response>
    • This function will be invoked on all PUT requests
  • onRequestDelete(contextEventContext) Response | Promise<Response>
    • This function will be invoked on all DELETE requests
  • onRequestHead(contextEventContext) Response | Promise<Response>
    • This function will be invoked on all HEAD requests
  • onRequestOptions(contextEventContext) Response | Promise<Response>
    • This function will be invoked on all OPTIONS requests

​​ env.ASSETS.fetch()

The env.ASSETS.fetch() function allows you to fetch a static asset. Requests to this will be to the pretty path not directly to the asset (i.e. if you had the path: functions/users/index.html, you will request /users/ instead of /users/index.html). This will run the header and redirect rules, so they will modify the response that is returned.

​​ Types

​​ EventContext

The following are the properties on the context object which are passed through on the onRequest methods:

  • request Request

    This is the incoming Request.

  • functionPath string

    This is the path of the request.

  • waitUntil(promisePromise<any>) void

    Read here for more info on waitUntil().

  • passThroughOnException() void

    Read here for more info on passThroughOnException(). Note that this will not work on an advanced mode project

  • next(input?Request | string, init?RequestInit) Promise<Response>

    Passes the request through to the next Function or to the asset server if no other Function is available.

  • env EnvWithFetch

  • params Params<P>

  • data Data

​​ EnvWithFetch

Holds the environment variables, secrets, and bindings for this Function. This also holds the ASSETS binding which is how you can fallback to the asset server.

​​ Params

Holds the values from the dynamic routes