Docs/Data & jobs/Edge functions

Edge functions

2 min read
On this page

An edge function is a single TypeScript file that runs on Deno and answers HTTP requests. You write the code in the dashboard, and Dokwe runs it in its own container behind an HTTPS URL.

How it works#

  • Runtime: Deno, using the denoland/deno:alpine image.
  • Code: one file, saved as main.ts. It can be up to 500,000 characters.
  • Trigger: HTTP requests only.
  • URL: https://<slug>-fn.<base-domain>.
  • Permissions: the function runs with --allow-net, --allow-env, and --allow-read. It can't write to disk, apart from a small /tmp.
  • Where it runs: on the Dokwe platform host. You can't deploy edge functions to your own servers yet.

Create a function#

  1. Open the form#

    Open Functions and start a new edge function.

  2. Name it#

    Enter a name (2–120 characters). The slug is filled in for you, and you can edit it. The slug sets the function's URL and must be unique.

  3. Write the handler#

    Start from the default code or paste your own. Your code must call Deno.serve:

    typescript
    Deno.serve((req: Request) => {
      const url = new URL(req.url);
    
      if (url.pathname === "/") {
        return new Response(JSON.stringify({ message: "Hello from edge function!" }), {
          headers: { "content-type": "application/json" },
        });
      }
    
      return new Response("Not Found", { status: 404 });
    });
  4. Deploy#

    Choose Create & deploy. The function moves from creating to running once its container starts. If it fails, the status changes to error and the function's page shows the reason under Fail reason.

Dokwe adds the port for you. If you call Deno.serve(handler), Dokwe rewrites it to listen on port 8000. If you pass your own options object (Deno.serve({ ... }, handler)), Dokwe leaves your code alone. In that case, set port: 8000 yourself so requests reach the function.

Call your function#

bash
curl https://<slug>-fn.<base-domain>/

The handler gets the full request, so you can route on url.pathname, read headers, and parse the body like in any Deno server.

Update the code#

Open the function, edit the code on the Code tab, and choose Save & deploy. Dokwe saves the new code and replaces the running container. Until you save, the editor shows Unsaved changes.

Stop, start, and delete#

  • Stop removes the running container and sets the status to stopped. The URL stops answering.
  • Start deploys the saved code again.
  • Delete stops the container and removes the function and its code.

Environment variables and secrets#

Add variables and secrets on the function's Settings tab, and read them in your code:

typescript
const secret = Deno.env.get("PAYSTACK_SECRET");
  • Values are encrypted (AES-256-GCM) and only passed to the function's container. The dashboard shows names, never values, so a value can't be read back; replace it instead.
  • Saving restarts a running function with the new values.
  • Names use letters, digits and _, and can't start with a digit. Names starting with DENO_ or DOKWE_, and PATH, HOME, HOSTNAME and PORT, are reserved.
  • A function can have up to 50 variables, each up to 32 KB.

Limits#

Each function container is limited to 2 GB of memory and 4,096 processes. It restarts automatically unless you stop it.

Logs#

The dashboard has no log viewer for edge functions. If a deploy fails, read the Fail reason on the function's page.

To run code on a schedule, use a cron job on a deployed project. Edge functions only respond to HTTP requests.