Search code examples
node.jsgoogle-cloud-functionsstrapi

STRAPI: Triggering a cloud function when a new collection type object is created


I am currently working with STRAPI (which is a headless CMS), I need to hit a cloud function whenever an object is created in a collection.

Is there any way to achieve this?

TIA.


Solution

  • You could use lifecycle hooks for that.

    // ./src/api/[api-name]/content-types/[api-name]/lifecycles.js
    
    module.exports = {
      beforeCreate(event) {
        const { data, where, select, populate } = event.params;
    
        // do something
      },
    
      afterCreate(event) {
        const { result, params } = event;
    
        // do something to the result;
      },
    };