Edge Functions

Run TypeScript server-side without provisioning a machine: webhooks, third-party calls, privileged writes, scheduled jobs.

Write and deploy

A function is a TypeScript file exporting a request handler. Name it with lowercase letters, digits, dashes or underscores (63 characters max); a few names are reserved by the platform. In the console editor, the save shortcut deploys the current version.

TypeScript

// index.ts — point d'entrée de la fonction
export default async (req: Request): Promise<Response> => {
  if (req.method !== 'POST') {
    return new Response('Méthode non autorisée', { status: 405 });
  }

  const { nom } = (await req.json()) as { nom?: string };
  const reponse = await fetch('https://exemple.ci/api/notification', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      // Secret de projet, jamais écrit dans le code
      Authorization: 'Bearer ' + Deno.env.get('EXEMPLE_CLE_TIERS'),
    },
    body: JSON.stringify({ message: 'Bonjour ' + (nom ?? 'le monde') }),
  });

  return Response.json({ ok: reponse.ok });
};

Extra files (shared folder, relative modules) ship with the function and are imported from the entry point.

Secrets

Keep third-party keys in project secrets, never in the code. The runtime also gives every function the project URL, the anonymous key, the service key, the schema and the project reference — enough to reach your database with no configuration.

  • Changing a secret redeploys the function: the new value is not visible before that.
  • The service key is a secret: a function that receives it can read and write everything, and must never hand it back to the client.

Test and observe

The test tab sends a real request and shows the response body; invocation logs keep the trail for diagnosing a 4xx/5xx. A function can require a session token: anonymous keys are still accepted by the gateway, but not as a user token.

Deploy from CI

To industrialise, deploy through the management API with a personal access token holding the functions scope: the same code then ships from your pipeline, without going through the console.

Next steps

Open the console