Skip to content

Custom API Endpoints

This allows you create your own API endpoints for complex requirements.

Getting Started

Create a new file in stack9/api-functions called example-api-endpoint.js

@prop context - object - context

// stack9/api-functions/example-cron-job.ts`
module.exports = {
  exec: async (context) => {
    const { db, services, user, query, logger } = context;
    const data = {};
    // ...

    return {
      success: true,
      data,
    };
  },
  timeout: 5000,
};

or in TypeScript

// stack9/server/api-functions/example-cron-job.ts`
class Custom extends CustomApi {
  constructor(private context: CustomApiContext<TQuery, TBody>) {
    super();
  }

  async exec(): Promise<CustomApiResponse> {
    const data = {};
    // ...

    return this.ok({ data }, 200);
  }
}

export default withApi(Custom, 5000);

Registration

Api functions do not require registration, the filename is used as slug for custom routes.

eg: HEAD, GET, POST and PUT /api/custom/example-api-endpoint will resolve to exec function.