For the complete documentation index, see llms.txt. This page is also available as Markdown.

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:

Property
Type
Description

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:

  1. Saved to the database after authentication

  2. Passed back to the API class on instantiation

  3. 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 credentials

  • scope - OAuth scopes

  • redirect_uri - OAuth callback URL

  • api_key - API key authentication

  • base_url - Base URL for API requests

  • Custom: UPPER_SNAKE_CASE pattern (e.g., CUSTOM_HEADER)

Encryption Configuration

Declare which credential fields need encryption beyond the core schema:

How it works:

  1. Module declares encryption.credentialFields array

  2. Framework adds data. prefix for database storage

  3. Fields merge with core encryption schema on startup

  4. All credential data transparently encrypted/decrypted

Core schema (auto-encrypted, no config needed):

  • access_token, refresh_token, id_token

  • username, password

  • domain

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

  • env properties use correct naming (snake_case)

  • No additional properties on strict objects

Best Practices

  1. Use snake_case for env properties - The schema enforces this pattern

  2. Keep moduleName simple - Use lowercase with hyphens (e.g., my-module)

  3. Persist minimal data - Only store what's needed for re-authentication

  4. Use core encryption - OAuth tokens are auto-encrypted; declare custom fields explicitly

  5. Test auth requests - Use a simple, fast endpoint for testAuthRequest

Last updated

Was this helpful?