Skip to content

Modules Overview

Microfn provides a set of built-in modules that extend your functions with powerful capabilities. These modules are automatically available in your functions without requiring any npm installations or configuration.

Microfn currently offers five core modules:

ModuleImportDescription
@microfn/aiimport { askAi } from "@microfn/ai"Send queries to AI language models
@microfn/kvimport kv from "@microfn/kv"Key-value storage that persists across executions
@microfn/secretimport secret from "@microfn/secret"Access environment variables and secrets
@microfn/fnimport fn from "@microfn/fn"Invoke other Microfn functions
@microfn/agentimport agent from "@microfn/agent"Create and manage AI agents

Microfn modules are injected at runtime with the necessary context and authentication. Each module:

  • Automatically authenticates using service account tokens
  • Knows your workspace context for proper data isolation
  • Handles errors gracefully with clear error messages
  • Provides TypeScript types for better development experience

Here’s a simple example using multiple modules together:

import kv from "@microfn/kv";
import { askAi } from "@microfn/ai";
import secret from "@microfn/secret";
export default async function main(input: any) {
// Get API configuration from secrets
const apiKey = await secret.get("API_KEY");
// Check if we have cached data
const cacheKey = `data:${input.id}`;
let data = await kv.get(cacheKey);
if (!data) {
// No cache, generate new data with AI
data = await askAi(`Generate data for ID: ${input.id}`);
// Cache for future use
await kv.set(cacheKey, data);
}
return { data, cached: !!data };
}

All modules provide full TypeScript support with type definitions:

import kv from "@microfn/kv";
interface UserPreferences {
theme: "light" | "dark";
language: string;
}
// Type-safe storage and retrieval
await kv.set<UserPreferences>("prefs", { theme: "dark", language: "en" });
const prefs = await kv.get<UserPreferences>("prefs");

Modules provide clear error messages for common issues:

try {
const required = await secret.getRequired("REQUIRED_SECRET");
} catch (error) {
// Error: Required secret REQUIRED_SECRET is not set
}

All module functions are async and can be used with modern async/await syntax:

export default async function main() {
const results = await Promise.all([
kv.get("key1"),
kv.get("key2"),
askAi("Generate a summary")
]);
return results;
}
  1. Import only what you need - Use specific imports to keep your code clean
  2. Handle errors appropriately - Wrap module calls in try-catch blocks when needed
  3. Use TypeScript - Take advantage of type definitions for better code quality
  4. Cache when possible - Use @microfn/kv to cache expensive operations
  5. Secure your secrets - Never hardcode sensitive data; use @microfn/secret

Some modules have usage limits based on your subscription tier:

  • AI queries - Limited per day based on subscription
  • KV storage - Storage size limits apply
  • Function invocations - Rate limits may apply

Check your subscription details for specific limits.

Explore each module in detail: