Dev.to AI πŸ€– Ai πŸ‘ 0 πŸ“– 5 min read

Your AI never sees your API keys: server code for websites, the safe way

Most websites need a little bit of backend sooner or later. Not a whole server. One form that emails you. One checkout button. One job that runs every morning. Each of those needs a secret key, and that is where it gets

Most websites need a little bit of backend sooner or later. Not a whole server. One form that emails you. One checkout button. One job that runs every morning.

Each of those needs a secret key, and that is where it gets awkward. The key can't go in the page, because anyone can read the page. So you set up a function somewhere, paste the key into its settings, and wire the form to it.

Now add an AI assistant to that picture. If it writes your function, it's easy for the key to end up in a chat, in a file, or in a log on its way past. We wanted the code, and the AI writing it, to never hold the real key at all.

This week we shipped that in Xodia as server tasks, together with a built-in database and visitor sign-in. Here's how it works.

A task is one file

A server task is a file in your site's api folder. Each file answers one address: api/contact.ts answers /api/contact. You export a function per HTTP method and return a standard Response.

Here's a trimmed version of the contact form task we ship ready-made:

type Context = { env: Record<string, string | undefined> };

export async function POST(request: Request, context: Context) {
  const { EMAIL_KEY, EMAIL_API_URL, EMAIL_FROM, EMAIL_TO } = context.env;
  if (!EMAIL_KEY || !EMAIL_API_URL || !EMAIL_FROM || !EMAIL_TO)
    return Response.json({ error: "not_set_up" }, { status: 503 });

  const { name, email, message, website } = await request.json();

  // A hidden field people never fill. Text in it means a bot.
  if (website) return Response.json({ sent: true });
  if (!name) return Response.json({ error: "Add your name." }, { status: 400 });
  if (!message) return Response.json({ error: "Write a message." }, { status: 400 });

  // Always to EMAIL_TO, never to an address the visitor sends.
  const res = await fetch(EMAIL_API_URL, {
    method: "POST",
    headers: { authorization: `Bearer ${EMAIL_KEY}`, "content-type": "application/json" },
    body: JSON.stringify({
      from: EMAIL_FROM,
      to: EMAIL_TO,
      subject: `New message from ${name}`,
      text: `${message}\n\nFrom: ${name} <${email}>`,
      reply_to: email,
    }),
  });
  if (!res.ok) return Response.json({ error: "Your message couldn't be sent." }, { status: 502 });
  return Response.json({ sent: true });
}

The full version also checks lengths, strips line breaks from the name so nobody can sneak in mail headers, and logs only a short, key-free line when the email service says no.

And from the page it's a plain fetch("/api/contact", ...). No SDK.

The key in your code is a stand-in

Look at EMAIL_KEY above. It looks like a normal environment variable, but the value your code gets is a stand-in, something like xodia-key.EMAIL_KEY..... It's useless on its own.

When you save a secret in Project settings, Keys and secrets, you also name the service it's for, like api.your-email-service.example. When your task makes a request to that host, Xodia swaps the stand-in for the real key on the way out. A request to any other address goes out with the stand-in.

So:

  • Your AI can write, read and change the task all day. It only ever sees the key's name.
  • A bug or a bad line of code that sends the key somewhere else sends a string that doesn't work.
  • Once saved, a secret is hidden from everyone, in Studio too. To change it you replace it.

Some keys can't work this way, like a webhook signing secret your code has to check itself. For those you turn on "The code reads this key itself", and then every change to the tasks needs a manager's OK before it can use that key. If the change came from your AI or from autosave, Studio shows a line asking you to read it and approve.

Checked before it goes live

Each new version of your server code is checked when you go live. Ready-made tasks you haven't changed go straight through. If something in your own code looks unsafe, that version is held until our team looks at it. Your preview keeps working, and the live site keeps its last checked version meanwhile.

The preview runs your tasks for real, a few seconds after you change a file in api. Recent calls shows each call's status, how long it took, which services it reached and what the task logged.

Automations: run it on a schedule

A task can also run without a visitor. Pick Run on a schedule and choose every hour (or every 15 or 5 minutes on bigger plans), every day at a time, certain days of the week, or a day of the month. Each run is a POST with no body.

The ready-made Daily check task uses this to open your pages every day and tell you when one breaks. The others are Send an email, AI answer (answer visitors' questions with your AI service), Stripe checkout, and Call a service.

Where the data goes

Server tasks cover the "talk to another service" part. For the "keep entries" part there is now a database built in.

You turn it on, pick where most of your visitors are, and add tables. Each table gets one rule in plain words, such as "Each person sees only their own" or "Everyone sees, only you change". There are separate test and live copies, so trying things on the canvas never touches real entries, and you can bring the data back to any point in the last 30 days or download it as an SQL file.

Visitor sign-in comes with it: email and password, or a code by email, with roles for people like staff.

If you already use Supabase, you can connect your own project instead, and your entries stay in your account.

Your AI can build all of it

Everything above works through the same MCP tools your own AI already uses to edit the site. It can add tables and fields, write and update tasks, and call a task in the preview to test it. It can't delete tables or entries, and it never sees a key's value: it tells you which key names to add, and you paste the values in yourself.

Why we built it this way

Xodia sites are real code you own, so a backend that only lives inside our UI would break that promise. A task is a normal TypeScript file in your repo. On Pro and Agency you can download your tasks to run on your own Cloudflare account.

The stand-in key was the part we cared about most. With AI writing more of the code, "never paste a key into the chat" is advice people forget. We'd rather make it impossible for the code to leak the real key in the first place.

Try it

Server tasks are on every plan, Free included, within its limits (schedules start on Starter). The guide walks through the first task: https://docs.xodia.ai/server-tasks?utm_source=devto&utm_medium=article&utm_campaign=post-0004

The whole Apps release, with six app templates to start from: https://xodia.ai/product/apps?utm_source=devto&utm_medium=article&utm_campaign=post-0004

How do you keep API keys away from your AI assistant today: environment files, a secrets manager, or just being careful?

πŸ“° Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.