Modules Overview
Microfn Modules
Section titled “Microfn Modules”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.
Available Modules
Section titled “Available Modules”Microfn currently offers five core modules:
| Module | Import | Description |
|---|---|---|
| @microfn/ai | import { askAi } from "@microfn/ai" | Send queries to AI language models |
| @microfn/kv | import kv from "@microfn/kv" | Key-value storage that persists across executions |
| @microfn/secret | import secret from "@microfn/secret" | Access environment variables and secrets |
| @microfn/fn | import fn from "@microfn/fn" | Invoke other Microfn functions |
| @microfn/agent | import agent from "@microfn/agent" | Create and manage AI agents |
How Modules Work
Section titled “How Modules Work”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
Basic Usage Example
Section titled “Basic Usage Example”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 };}Module Features
Section titled “Module Features”Type Safety
Section titled “Type Safety”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 retrievalawait kv.set<UserPreferences>("prefs", { theme: "dark", language: "en" });const prefs = await kv.get<UserPreferences>("prefs");Error Handling
Section titled “Error Handling”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}Async/Await Support
Section titled “Async/Await Support”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;}Best Practices
Section titled “Best Practices”- Import only what you need - Use specific imports to keep your code clean
- Handle errors appropriately - Wrap module calls in try-catch blocks when needed
- Use TypeScript - Take advantage of type definitions for better code quality
- Cache when possible - Use
@microfn/kvto cache expensive operations - Secure your secrets - Never hardcode sensitive data; use
@microfn/secret
Usage Limits
Section titled “Usage Limits”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.
Next Steps
Section titled “Next Steps”Explore each module in detail:
- AI Module - Generate text and analyze data with AI
- Key-Value Storage - Persist data across function executions
- Secrets Management - Securely access configuration values
- Function Invocation - Build composable function workflows
- AI Agents - Create autonomous AI agents