ai-prompt-template Edge Function — Ai
AIFills {{variable}} placeholders in a prompt template string, with optional strict mode to catch missing variables.
Edge function ai-prompt-template Fills {{variable}} placeholders in a prompt template string, with optional strict mode to catch missing variables.. 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/ai-prompt-template 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: "ai-prompt-template",
args: {
"template": "example_template",
"variables": null,
"strict": true
}
}) ai-prompt-template — Fill prompt templates with dynamic variables
Replaces
{{variableName}}placeholders in a prompt template string with supplied values, with optional strict mode to catch missing variables before sending to an LLM.
API
POST /api/ai-prompt-template
Request body
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
template |
string | ✅ | — | Prompt string containing {{variableName}} placeholders |
variables |
object | ✅ | — | Key-value map of placeholder names to replacement values (string, number, boolean) |
strict |
boolean | ❌ | true |
If true, throw when any placeholder is unresolved |
Success response (200)
{
"success": true,
"data": {
"prompt": "Hello, Alice! Your task is to summarize the document.",
"resolvedCount": 2,
"unresolvedPlaceholders": []
}
}
Error responses
| Code | HTTP | When |
|---|---|---|
INVALID_INPUT |
400 | Missing required field or wrong type |
INTERNAL_ERROR |
500 | Unresolved placeholder in strict mode |
Usage
cURL
curl -X POST "$FUNCTION_URL" \
-H "Content-Type: application/json" \
-d '{
"template": "Hello, {{name}}! Your task is to {{task}}.",
"variables": { "name": "Alice", "task": "summarize the document" }
}'
TypeScript / JavaScript (HTTP)
const response = await fetch(FUNCTION_URL, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
template: 'Translate the following to {{language}}:\n\n{{text}}',
variables: { language: 'French', text: 'Good morning!' },
}),
});
const { data } = await response.json();
console.log(data.prompt);
Direct import (Node / Bun / Deno)
import { aiPromptTemplate } from '@aerostack/functions/ai-prompt-template';
const result = aiPromptTemplate({
template: 'You are a {{role}}. Answer the following: {{question}}',
variables: { role: 'helpful assistant', question: 'What is RAG?' },
});
console.log(result.prompt);
Use Cases
- Building a prompt library where each entry uses
{{placeholders}}for user-supplied data before sending to an LLM API. - Generating personalised email drafts or support replies by filling in customer name, product, and issue variables.
- Validating prompt templates during CI — set
strict: trueand check that all placeholders resolve before deploying.
Notes
- Placeholder syntax is
{{variableName}}— double curly braces, alphanumeric + underscores only. - Values are coerced to strings; numbers and booleans are supported.
- When
strict: false, unresolved placeholders are left as-is in the output and listed inunresolvedPlaceholders. - Consecutive blank lines in the resolved prompt are collapsed to a maximum of two.
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 AI Functions
Browse AI Functions →ai-chunk-overlap
by @navin
Splits text into overlapping chunks with configurable size, overlap, and boundary snapping (char, word, sentence) for RAG and embedding pipelines.
ai-context-window-fit
by @navin
Trims a conversation message array to fit within a model's context window using configurable strategies, without making any API calls.
ai-cost-estimate
by @navin
Calculates the API cost for an LLM request given a model name, prompt token count, and completion token count, supporting multiple currencies.
ai-extract-keywords
by @navin
Extracts the top N keywords from text using TF-IDF inspired scoring with built-in English stopword filtering, no external API calls required.
ai-guardrail-injection-detect
by @navin
Scores text for common prompt injection attack patterns including role overrides, instruction leaking, and jailbreak attempts.
ai-language-detect
by @navin
Detects the natural language of a text string using character trigram frequency analysis, supporting 13 languages with no external API calls.
Frequently asked questions
What does the ai-prompt-template function do? +
ai-prompt-template is a serverless edge function for ai automation written in aerostack. Deploy it to Cloudflare Workers via your Aerostack workspace.
How do I deploy the ai-prompt-template function? +
Install the Aerostack CLI and run: ```bash aerostack deploy function @navin/ai-prompt-template ``` It will be live on Cloudflare Workers in seconds.
What runtime does ai-prompt-template use? +
ai-prompt-template runs on aerostack on the Cloudflare Workers edge runtime — zero cold starts, globally distributed.
Can I customise the ai-prompt-template function? +
Yes. Fork the function from your Aerostack dashboard, modify the source, and redeploy. All changes are version-controlled.