I am using Strapi CMS for my data handling with a NoSQL database. So, what I am trying to do is to publish the blog on Medium also when I publish it on Strapi CMS.
I have all the credentials for publishing it on medium using API.
So, the question is how to achieve this, how to perform a certain action in Strapi CMS when a post is created or updated, so that I can get the data and send to Medium via POST request.
Advertisement
Answer
So after some research and instruction by Ghosh I came to know about the webhooks provided by Strapi by default.
These hooks are somewhat same as React Lifecycle Hooks
More about hooks: Here
So for my case I have to add a hook which trigger some action after blog post is created.
So, in api/blog/models/{blog}.js
, I added this code
JavaScript
x
25
25
1
"use strict";
2
const fetch = require("node-fetch");
3
4
module.exports = {
5
lifecycles: {
6
async afterCreate(data) {
7
const body = {
8
title: data.title,
9
contentFormat: "markdown",
10
content: data.blog_data,
11
tags: [],
12
};
13
const response = await fetch(
14
`https://api.medium.com/v1/users/${process.env.CLIENT_SECRET}/posts?accessToken=${process.env.ACCESS_TOKEN}`,
15
{
16
method: "POST",
17
headers: { "Content-Type": "application/json" },
18
body: JSON.stringify(body),
19
}
20
);
21
},
22
},
23
};
24
25