Trigger webhook event from API

System Information
  • Strapi Version: 4.14.5
  • Operating System: macOS/Amazon Linux
  • Database: postgres
  • Node Version:
  • NPM Version:
  • Yarn Version:

Hello,
I have a question regarding triggering webhook events programmatically. I can’t seem to find it. We don’t feed our website with content from Strapi directly, but have a middleware service that feeds content to our website. We are using a webhook to notify the service that a new article is available so that it can pull it to its database.

I am trying to find a solution for our QA engineers who would like to write automated tests that would trigger article publishing via Strapi content API and then check if the new entry in the middleware database exists and is as expected.
I can create new articles in Strapi via API, but there is no documentation about publishing. I’ve seen some comments about setting the publishedAt field with a timestamp that would mark it as published in Strapi. It works fine in Strapi as the article appears published, but it doesn’t seem to trigger a webhook event.

Does anyone know how I can trigger a webhook publish event when creating an article through API?

from the admin you can do the following, but this will be on any create event for any content-type, handle the rest from your webhook but if you want, use a lifecyles for the content-type and post it to a queue to send that request out.

Thanks @StephaneP

I’m sorry that I didn’t mention I already have a webhook set this way. I do have working webhook events for publish, update and unpublish and the webhook service receives events when articles are created via the admin ui.

I need the webhook service to receive update, publish and unpublish events when content entries are created via API but it doesn’t seem they do.
Is there a way to trigger a webhook event programmatically?

Update: I had a bug in code that was preventing Strapi from sending webhook events when ithey were sent through API, but it was hard to find as they were not directly related to an article creation.

Now I can see create and update events, still no publish events. I need to send an entry.publish event, though.

Update:

I did some digging through strapi console and source code and in the end added a create method in the content type controller with extra logic to send the publish event.

async create(ctx) {

    const response = await super.create(ctx);

    console.log(response.data?.attributes?.publishedAt)
    if(response.data?.attributes?.publishedAt) {

      const info: Record<string, unknown> = {
        "model": "article",
        "entry": {
          id: response.data.id,
          ...response.data.attributes
        }
      }

      await strapi.webhookRunner.executeListener({event: "entry.publish", info: info});

    }
    return response;
  }
1 Like