Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Post to Hashnode after new post is created in Strapi Backend
'use strict';
const fetch = require('node-fetch');
/**
* Read the documentation (https://strapi.io/documentation/developer-docs/latest/concepts/models.html#lifecycle-hooks)
* to customize this model
*/
module.exports = {
lifecycles: {
async afterCreate(data) {
fetch('https://api.hashnode.com', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: YOUR_AUTHORIZATION_TOKEN,
},
body: JSON.stringify({
query: `mutation createPublicationStory($input: CreateStoryInput!, $hideFromHashnodeFeed: Boolean, $publicationId: String!){
createPublicationStory(
input: $input,
hideFromHashnodeFeed: $hideFromHashnodeFeed,
publicationId: $publicationId
) {
success
code
message
}
}`,
variables: {
input: {
title: data.Title,
contentMarkdown: data.Article,
tags: [
{
"_id": "56744723958ef13879b952d7",
"name": "Developer"
},
{
"_id": "56744720958ef13879b947ce",
"name": "Tutorial"
},
],
},
publicationId: YOUR_BLOGS_PUBLICATION_ID,
hideFromHashnodeFeed: false
},
}),
})
.then(res => res.json())
.then(res => console.log(JSON.stringify(res)))
},
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment