API Module Definition and Functions
This document describes the API module definition structure used by the Frigg Framework. API modules provide the connection layer between Frigg and external APIs.
Schema Reference
The canonical JSON Schema is at packages/schemas/schemas/api-module-definition.schema.json.
Required Properties
Every API module definition must include these three properties:
moduleName
string
Unique identifier for the module (pattern: ^[a-zA-Z][a-zA-Z0-9_-]*$)
getName
function
Returns the module name
requiredAuthMethods
object
Authentication method implementations
Complete Definition Structure
const { MyApi } = require('./api');
const Definition = {
// Required: API class
API: MyApi,
// Required: Module identifier
moduleName: 'my-module',
// Required: Function returning module name
getName: () => 'my-module',
// Required: Authentication methods
requiredAuthMethods: {
getToken: async (api, params) => { /* ... */ },
getEntityDetails: async (api, callbackParams, tokenResponse, userId) => { /* ... */ },
getCredentialDetails: async (api, userId) => { /* ... */ },
testAuthRequest: async (api) => { /* ... */ },
apiPropertiesToPersist: {
credential: ['access_token', 'refresh_token'],
entity: ['tenantId']
}
},
// Optional: Environment configuration
env: {
client_id: process.env.MY_CLIENT_ID,
client_secret: process.env.MY_CLIENT_SECRET,
scope: 'read write',
redirect_uri: process.env.MY_REDIRECT_URI,
base_url: process.env.MY_BASE_URL // Note: snake_case
},
// Optional: Module-level encryption for custom credential fields
encryption: {
credentialFields: ['api_key', 'webhook_secret']
}
};
module.exports = { Definition, MyApi };Required Auth Methods
getToken
Retrieves and sets authentication tokens. For OAuth2, this typically exchanges an authorization code for tokens:
For session-based auth:
getEntityDetails
Retrieves details about the authorized user/organization. Returns identifiers for uniqueness and details for display:
getCredentialDetails
Similar to getEntityDetails, but for credential lookup:
testAuthRequest
A simple request to verify authentication is working:
apiPropertiesToPersist
Defines which API properties to save to the database:
These properties are:
Saved to the database after authentication
Passed back to the API class on instantiation
Available via
api.propertyName
Environment Configuration
The env object maps environment variables to API configuration. Use snake_case for property names:
Allowed properties:
client_id,client_secret- OAuth credentialsscope- OAuth scopesredirect_uri- OAuth callback URLapi_key- API key authenticationbase_url- Base URL for API requestsCustom:
UPPER_SNAKE_CASEpattern (e.g.,CUSTOM_HEADER)
Encryption Configuration
Declare which credential fields need encryption beyond the core schema:
How it works:
Module declares
encryption.credentialFieldsarrayFramework adds
data.prefix for database storageFields merge with core encryption schema on startup
All credential data transparently encrypted/decrypted
Core schema (auto-encrypted, no config needed):
access_token,refresh_token,id_tokenusername,passworddomain
Common patterns:
Complete OAuth2 Example
Session-Based Auth Example
Validation
Use frigg validate to check your module definition against the schema:
The validator checks:
Required properties are present
Property types match schema
envproperties use correct naming (snake_case)No additional properties on strict objects
Best Practices
Use snake_case for
envproperties - The schema enforces this patternKeep
moduleNamesimple - Use lowercase with hyphens (e.g.,my-module)Persist minimal data - Only store what's needed for re-authentication
Use core encryption - OAuth tokens are auto-encrypted; declare custom fields explicitly
Test auth requests - Use a simple, fast endpoint for
testAuthRequest
Related Documentation
JSON Schema - Canonical schema definition
Integration Patterns Guide - Sync, queue, and webhook patterns
Encryption README - Field-level encryption details
Last updated
Was this helpful?
