# Extending integration

# Adding new API endpoints

To register a new API endpoint, you can register a custom extension and use the extendApiMethods property. API endpoints cannot be registered directly. Let's look at an example:

module.exports = {
  integrations: {
    '{INTEGRATION_NAME}': {
      // ...
      extensions: (extensions) => [
        ...extensions,
        {
          name: 'extension-name',
          extendApiMethods: {
            customMethod: async (context, params) => {
              const response = await context.client.mutate({
                mutation: gql`${someQuery.query}`,
                variables,
                fetchPolicy: 'no-cache', // in most cases you do not want to cache response in the Apollo
                /**
                 * This is a required object and must be attached to every graphql endpoint
                 * with req and res node objects inside.
                 * If you are using custom client then you can omitt this.
                 */
                context: {
                  req: context.req,
                  res: context.res
                }
              });
            }
          }
        }
      ],
    }
  }
};

Because this is an abstract example that applies to all integrations, we intentionally used {INTEGRATION_NAME} as the name of the integration. In this example, we are registering customMethod in extendApiMethods that creates a new /api/{INTEGRATION_NAME}/customMethod endpoint.

This method accepts two parameters:

  • context which includes:
    • config - integration configuration,
    • client - API client created in packages/api-client/src/index.server.ts,
    • req - HTTP request object,
    • res - HTTP response object,
    • extensions - extensions registered within integration,
    • customQueries - custom GraphQL queries registered within integration (used only with GraphQL),
    • extendQuery - helper function for handling custom queries (used only with GraphQL).
  • params - parameters passed.