> ## Documentation Index
> Fetch the complete documentation index at: https://docs.optidev.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Edge Functions

> Run server-side code and store API keys for your app

## What are Edge Functions

Edge Functions are small pieces of code that run on the server instead of in the browser. Use them for anything you can't (or shouldn't) do client-side:

* **Payment processing** — Stripe, PayPal, etc.
* **Sending emails** — confirmations, notifications, newsletters
* **API integrations** — HubSpot, Zendesk, custom APIs
* **Protected operations** — anything that needs a secret API key

They run globally, close to your users, so they're fast everywhere.

***

## Creating Functions with OptiDev Agent

Describe what you need and OptiDev Agent writes the function, wires up any required services, and deploys it.

> *"Create an API endpoint that accepts credit card payments with Stripe and saves the order to the database"*

> *"Send a welcome email when a new user signs up"*

> *"Fetch customer data from HubSpot when given an email"*

***

## The Edge Functions Tab

Split into two panels: **Functions on the left**, **Secrets on the right** (stacked on mobile).

The function panel has two sections:

* **Workspace Functions** — code in your project files. Each row shows a **Live** badge (green) if deployed, or **Not deployed** (gray).
* **Deployed Functions** — what's currently running. Each row shows the version, last-updated date, and a copy-able public URL.

If you don't have any functions yet, click **Create Sample** to generate a working "hello world" you can deploy immediately.

***

## Deploying a Function

Click **Deploy** (or **Redeploy** if you've changed the code) next to a workspace function. Takes a few seconds. Or just ask the agent: *"Deploy my payment function."*

When OptiDev Agent creates a function, it usually deploys it automatically right after.

***

## Viewing Logs

Click **Logs** on any deployed function to jump to the **Logs** tab pre-filtered to that function. You can switch between **Invocations** (per-request details — method, status, duration) and **Console logs**.

See [Logs](/optidev-cloud/logs) for more.

***

## Deleting a Function

Click the **trash icon** on a deployed function row and confirm.

<Warning>
  Deletion is immediate. Any app or webhook calling the function will start getting errors. Make sure nothing is using it first.
</Warning>

***

## Secrets

The right panel of the Edge Functions tab is where you store sensitive values your functions need — API keys, third-party tokens, connection strings. Secrets are encrypted at rest and only available to server-side code, never the browser.

### Adding secrets

The **Add new** form is always visible. Type a name (auto-uppercased, e.g. `STRIPE_SECRET_KEY`), paste the value (use the eye icon to peek), click **Add row** to stage more in the same save, then click **Save**. Names must be `UPPERCASE_WITH_UNDERSCORES_AND_DIGITS` and can't be reused.

### Deleting a secret

Click the **trash icon** next to a secret and confirm.

<Note>
  Secrets are **write-only**. You can see the name and last-updated date, but not the value. To change a value, delete and re-add.
</Note>

<Warning>
  Deleting a secret is immediate. Any function reading it will start failing until you add it back.
</Warning>

### Auto-available variables

`SUPABASE_URL` and `SUPABASE_ANON_KEY` are always available inside Edge Functions — no need to add them as secrets. For privileged operations (admin DB access, user management), let OptiDev Agent write the code so you never have to handle those credentials yourself.

### Using a secret

Just reference the name when asking the agent: *"Create a Stripe payment function using my STRIPE\_SECRET\_KEY secret."* The agent reads it via `Deno.env.get('STRIPE_SECRET_KEY')` so the value stays server-side.

***

## Common Use Cases

* **Webhook handlers** — *"Receive Stripe webhooks and update order status."*
* **Form submissions** — *"Receive contact form submissions and send them to my email."*
* **Data processing** — *"Take a CSV upload and import it into the products table."*
* **Scheduled tasks** — *"Run daily to send appointment reminder emails."*
* **Third-party sync** — *"Sync new customers to my Mailchimp list."*

***

## For Developers

<Accordion title="Technical Reference">
  ### Function file structure

  Edge Functions are TypeScript/JavaScript on Deno. Each lives at:

  ```
  supabase/functions/function-name/index.ts
  ```

  ### Function URL

  ```
  https://your-project.sb.optidev.ai/functions/v1/function-name
  ```

  ### Invoking from your app

  ```typescript theme={null}
  const { data, error } = await supabase.functions.invoke('function-name', {
    body: { amount: 2500 }
  })
  ```

  ### Accessing secrets

  ```typescript theme={null}
  const stripeKey = Deno.env.get('STRIPE_SECRET_KEY')
  ```

  Secrets are available immediately after saving — no redeploy needed. All values are AES-256-GCM encrypted at rest, scoped per workspace.

  ### Logging

  `console.log()`, `console.error()`, etc. show up in the Logs tab under Console Logs.

  ### CORS

  The sample function template includes CORS headers. Reuse that pattern for any function called from the browser.

  ### Naming conventions

  Use `UPPERCASE_WITH_UNDERSCORES`. Be descriptive (`STRIPE_SECRET_KEY`, not `SK`) and prefix with the service (`HUBSPOT_API_KEY`).
</Accordion>
