Aerostack
security

sec-api-key-generate Edge Function — Security

Security

Generates a cryptographically random API key with a custom prefix using a base62 alphabet — no ambiguous characters, URL-safe.

navin @navin verified
Updated Mar 12, 2026
GitHub

Edge function sec-api-key-generate Generates a cryptographically random API key with a custom prefix using a base62 alphabet — no ambiguous characters, URL-safe.. Deployed on Cloudflare Workers — zero cold starts, globally distributed. Mount it via your Aerostack workspace to call it from any AI agent.

terminal — aerostack cli
$ npx aerostack add navin/sec-api-key-generate
smart_toy

Use with AI Assistants

MCP

Connect Claude, Cursor, or any MCP-compatible client — then call this function by slug

① Add MCP Server

Add this once — access all Aerostack functions from your AI tool.

claude_desktop_config.json
{
  "mcpServers": {
    "aerostack": {
      "url": "https://mcp.aerostack.dev",
      "type": "http"
    }
  }
}

② Call this function

Ask your AI to use the call_function tool with this slug:

call_function
call_function({
  slug: "sec-api-key-generate",
  args: {
    "prefix": "example_prefix",
    "length": 32
  }
})

sec-api-key-generate — Generate prefixed API keys

Generates a cryptographically random API key with a human-readable prefix using base62 encoding — no ambiguous characters (0/O, 1/l/I), URL-safe.


API

POST /api/sec-api-key-generate

Request body

Field Type Required Default Description
prefix string Key prefix, e.g. sk_live, pk_test, webhook
length number 32 Entropy length (16–64 characters)

Success response (200)

{
  "success": true,
  "data": {
    "key": "sk_live_xK7mNpQ2rT8vWbYhGjF3sLcEuDiAzM9n",
    "prefix": "sk_live",
    "entropy": "xK7mNpQ2rT8vWbYhGjF3sLcEuDiAzM9n"
  }
}

Error responses

Code HTTP When
INVALID_INPUT 400 Missing prefix or length out of range
INTERNAL_ERROR 500 Unexpected error

Usage

cURL
curl -X POST "$FUNCTION_URL" \
  -H "Content-Type: application/json" \
  -d '{"prefix": "sk_live", "length": 32}'
TypeScript / JavaScript (HTTP)
const response = await fetch(FUNCTION_URL, {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ prefix: 'sk_live', length: 32 }),
});
const { data } = await response.json();
// Show data.key to the user ONCE, store hash(data.entropy) in DB
Direct import (Node / Bun / Deno)
import { generateApiKey } from '@aerostack/functions/sec-api-key-generate';

const { key, prefix, entropy } = generateApiKey({ prefix: 'sk_live', length: 32 });

Use Cases

  • Generating API keys when a user creates a new integration or service account
  • Issuing prefixed tokens that make key type immediately identifiable (e.g. sk_live vs sk_test)
  • Creating webhook secrets for secure delivery verification
  • Generating one-time invite codes or access tokens

Notes

  • Alphabet: 23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz — removes 0, O, 1, l, I to avoid visual confusion
  • Uses crypto.getRandomValues with modulo-bias rejection — cryptographically uniform
  • Show the full key to the user exactly once — store the hash of entropy in your database, not the plaintext
  • The underscore separator between prefix and entropy is always _

Metadata

upgrade Version 1.0.0
gavel License MIT
language Language typescript
cloud Provider aerostack

Tags

security
deployed_code

Build and publish your own functions

Write a TypeScript function, deploy it to the edge, and share it with thousands of developers — in minutes.

More Security Functions

Browse Security Functions →

Frequently asked questions

What does the sec-api-key-generate function do? +

sec-api-key-generate is a serverless edge function for security automation written in aerostack. Deploy it to Cloudflare Workers via your Aerostack workspace.

How do I deploy the sec-api-key-generate function? +

Install the Aerostack CLI and run: ```bash aerostack deploy function @navin/sec-api-key-generate ``` It will be live on Cloudflare Workers in seconds.

What runtime does sec-api-key-generate use? +

sec-api-key-generate runs on aerostack on the Cloudflare Workers edge runtime — zero cold starts, globally distributed.

Can I customise the sec-api-key-generate function? +

Yes. Fork the function from your Aerostack dashboard, modify the source, and redeploy. All changes are version-controlled.