sec-api-key-generate Edge Function — Security
SecurityGenerates a cryptographically random API key with a custom prefix using a base62 alphabet — no ambiguous characters, URL-safe.
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.
npx aerostack add navin/sec-api-key-generate Use with AI Assistants
MCPConnect 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.
{
"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({
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_livevssk_test) - Creating webhook secrets for secure delivery verification
- Generating one-time invite codes or access tokens
Notes
- Alphabet:
23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz— removes0,O,1,l,Ito avoid visual confusion - Uses
crypto.getRandomValueswith modulo-bias rejection — cryptographically uniform - Show the full
keyto the user exactly once — store the hash ofentropyin your database, not the plaintext - The underscore separator between prefix and entropy is always
_
Metadata
Tags
Publisher
@navin verified
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 →sec-cors-validate
by @navin
Validates a request Origin against an allow list and returns the correct CORS response headers to set — supports wildcards and credentials.
sec-csp-generate
by @navin
Generates a Content-Security-Policy header value from a structured directives object — supports report-only mode and report-uri.
sec-decrypt-aes
by @navin
Decrypts an AES-256-GCM encrypted bundle produced by sec-encrypt-aes — key is derived via SHA-256, auth tag is verified automatically.
sec-encrypt-aes
by @navin
Encrypts a string with AES-256-GCM using Web Crypto — key is derived via SHA-256, output is a portable IV:ciphertext bundle.
sec-hash-sha256
by @navin
Hashes a string using SHA-256 via the Web Crypto API. Supports hex and base64 output. Zero dependencies.
sec-hmac-sign
by @navin
Signs a string payload with HMAC-SHA256 or HMAC-SHA512 using Web Crypto — outputs a hex-encoded signature.
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.