Skip to content

Configuration Overview

Microfn provides flexible configuration options for your serverless functions, allowing you to manage environment variables, secrets, and NPM package dependencies. Proper configuration ensures your functions have access to the resources and credentials they need while maintaining security.

Microfn offers two main configuration areas:

Configuration TypeDescriptionUse Cases
Environment Variables & SecretsSecure storage for sensitive dataAPI keys, tokens, credentials
NPM PackagesExternal JavaScript dependenciesLibraries, SDKs, utilities

Store sensitive configuration securely:

import secret from "@microfn/secret";
const apiKey = await secret.getRequired("API_KEY");
const dbUrl = await secret.get("DATABASE_URL", "localhost:5432");
  • Stored encrypted in workspace
  • Accessed via @microfn/secret module
  • Never exposed in logs or responses
  • Environment-specific values supported

Add NPM packages to your functions:

// After adding 'lodash' package to workspace
import _ from "lodash";
export default async function main(input) {
const sorted = _.orderBy(input.data, ['date'], ['desc']);
return { sorted };
}
  • Add packages through dashboard or API
  • Version management supported
  • Deployed as Lambda layers
  • Automatic dependency resolution

The easiest way to manage configuration:

  1. Navigate to your workspace
  2. Go to Settings
  3. Configure secrets or packages
  4. Changes apply immediately

Programmatically manage configuration:

Terminal window
# Add a secret
curl -X POST https://microfn.dev/api/workspaces/username/workspace/secrets \
-H "Authorization: Bearer TOKEN" \
-d '{"key": "API_KEY", "value": "secret-value"}'
# Add a package
curl -X POST https://microfn.dev/api/workspaces/username/workspace/packages \
-H "Authorization: Bearer TOKEN" \
-d '{"name": "lodash", "version": "4.17.21"}'

Manage configuration through code:

configuration.ts
export const config = {
secrets: [
{ key: "API_KEY", description: "Main API key" },
{ key: "DATABASE_URL", description: "Database connection" }
],
packages: [
{ name: "lodash", version: "^4.17.21" },
{ name: "axios", version: "^1.0.0" }
]
};
import secret from "@microfn/secret";
export default async function main(input) {
const env = await secret.get("ENVIRONMENT", "development");
const config = {
development: {
apiUrl: "https://dev.api.example.com",
debug: true
},
production: {
apiUrl: "https://api.example.com",
debug: false
}
};
return config[env] || config.development;
}

Prefix secrets for different environments:

// Secrets configured:
// DEV_API_KEY, PROD_API_KEY, STAGING_API_KEY
const env = await secret.get("ENV", "DEV");
const apiKey = await secret.getRequired(`${env}_API_KEY`);
  1. Never hardcode secrets in function code
  2. Use descriptive names for configuration values
  3. Rotate secrets regularly through the dashboard
  4. Limit secret access to necessary functions only
  5. Audit secret usage through execution logs
  1. Pin package versions for consistency
  2. Minimize dependencies to reduce cold start time
  3. Test package compatibility before production
  4. Update packages regularly for security patches
  5. Use native modules when available
// Group related configuration
const dbConfig = {
host: await secret.get("DB_HOST"),
port: await secret.get("DB_PORT", "5432"),
name: await secret.get("DB_NAME"),
user: await secret.get("DB_USER"),
password: await secret.getRequired("DB_PASSWORD")
};
const apiConfig = {
key: await secret.getRequired("API_KEY"),
url: await secret.get("API_URL"),
timeout: parseInt(await secret.get("API_TIMEOUT", "5000"))
};
import secret from "@microfn/secret";
async function validateConfiguration() {
const required = [
"API_KEY",
"DATABASE_URL",
"AUTH_TOKEN"
];
const missing = [];
for (const key of required) {
if (!await secret.has(key)) {
missing.push(key);
}
}
if (missing.length > 0) {
throw new Error(`Missing required secrets: ${missing.join(", ")}`);
}
}
export default async function main(input) {
await validateConfiguration();
// Function logic here
return { success: true };
}
interface Config {
apiKey: string;
apiUrl: string;
timeout: number;
retries: number;
}
async function loadConfig(): Promise<Config> {
return {
apiKey: await secret.getRequired("API_KEY"),
apiUrl: await secret.get("API_URL", "https://api.example.com"),
timeout: parseInt(await secret.get("TIMEOUT", "5000")),
retries: parseInt(await secret.get("RETRIES", "3"))
};
}
export default async function main(input) {
const config = await loadConfig();
// Use type-safe config
const response = await fetchWithRetry(
config.apiUrl,
config.apiKey,
config.retries
);
return response;
}
FeatureFree TierPro TierEnterprise
Secrets per workspace10100Unlimited
Package dependencies550Unlimited
Secret size1 KB10 KB100 KB
Total package size10 MB100 MB1 GB
import secret from "@microfn/secret";
export default async function exportConfig() {
const allSecrets = await secret.getWithPrefix("");
// Mask sensitive values
const masked = {};
for (const [key, value] of Object.entries(allSecrets)) {
masked[key] = value.substring(0, 3) + "***";
}
return {
secrets: Object.keys(masked),
packages: await getWorkspacePackages(),
exported: new Date()
};
}
// config.json
{
"secrets": {
"API_KEY": {
"description": "Primary API key",
"required": true
},
"DATABASE_URL": {
"description": "Database connection string",
"required": true
}
},
"packages": {
"lodash": "^4.17.21",
"axios": "^1.0.0",
"date-fns": "^2.29.0"
}
}
IssueSolution
Secret not foundCheck spelling and SECRET_ prefix
Package not availableEnsure package is added and deployed
Configuration not updatingClear cache or redeploy function
Size limits exceededRemove unused packages or optimize
export default async function debugConfig() {
return {
hasApiKey: await secret.has("API_KEY"),
environment: await secret.get("ENVIRONMENT", "not set"),
secretCount: Object.keys(await secret.getWithPrefix("")).length,
packages: process.env.NODE_PATH
};
}

Dive deeper into specific configuration areas: