GUIDE

Store API keys as Microfn function secrets

Updated September 09, 2026 By microfn 2 min read
secrets security api

API keys belong in per-function secrets, not source code, JSON input, logs, or agent prompts. Microfn injects configured values at runtime and the @microfn/secret module reads them by name.

Add the secret

Open your function’s Secrets panel and add a key such as WEATHER_API_KEY. Paste the value there and save it. Secret names are case-sensitive.

Read it in the function

import secret from "@microfn/secret";

export async function main(input: { city?: string }) {
  const apiKey = await secret.getRequired("WEATHER_API_KEY");
  const city = input.city || "Tokyo";
  const url = new URL("https://api.example.com/weather");
  url.searchParams.set("city", city);

  const response = await fetch(url, {
    headers: { Authorization: `Bearer ${apiKey}` },
  });

  if (!response.ok) {
    throw new Error(`Weather API returned ${response.status}`);
  }

  return response.json();
}

getRequired stops the execution with a clear error when the value is missing. Use get only when the secret is genuinely optional.

Work with an agent safely

An MCP-connected agent can write code that refers to the secret name. Give it the name, expected use, and API documentation, but add the value through Microfn’s secret UI.

Example agent conversation
You

Update my weather function to read WEATHER_API_KEY with @microfn/secret. Do not put the key in source or logs.

Agent

I’ll update the function to call getRequired("WEATHER_API_KEY") and leave the value for you to configure in the Secrets panel.

update_function

Updated weather without embedding a credential.

Rotate a key at its provider and update the Microfn secret when it is exposed. Review calling an external API for response handling.

Related guides

All guides