DEV Community

Cover image for I Built an Node.js Application to Publish my Articles on Dev.to🧙‍♂️🔮
Arindam Majumder
Arindam Majumder

Posted on

I Built an Node.js Application to Publish my Articles on Dev.to🧙‍♂️🔮

Introduction

Technical Writing is (still) one of the Key Aspects of Software Development. And Most of us post our Content on Hashnode, Medium, DevTo.

Recently, I built a project which will simplify the publishing articles on DevTo. And you can build it too!!

Don't have much knowledge about that right?

GIF

No worries!

In this article, I will show you How to post an Article to DevTo from a Nodejs Application.

So, without delaying further, Let's START!

Project Setup

1. Create Node.js Project:

To start our project, we need to set up a Node.js environment. So, let's create a node project. Run the Following command in the Terminal.

npm init
Enter fullscreen mode Exit fullscreen mode

This will initialize a new Node.js project.

2. Install Dependencies:

Now, We'll install the required dependencies of our project.

npm install express axios dotenv
Enter fullscreen mode Exit fullscreen mode

This will install the following packages:

  • express: a popular web framework for Node.js

  • dotenv: loads environment variables from a .env file.

  • axios: HTTP client for Node.js

3. Setup Environment Variables:

Next, we'll create a .env folder to securely store our sensitive information such as API credentials.

//.env
PORT = YOUR_PORT || 8000
API_KEY = YOUR_API_KEY
Enter fullscreen mode Exit fullscreen mode

4. Create Express Server:

Now, we'll create a index.js file in the root directory and set up a basic express server. See the following code:

const express = require("express");
const dotenv = require("dotenv");

dotenv.config();

const app = express();

const port = process.env.PORT || 8000;

app.get("/", (req, res) => {
  res.send("Hello World");
});

app.listen(port, () => {
  console.log(`Server running on port ${port}`);
});
Enter fullscreen mode Exit fullscreen mode

Here, We're using the "dotenv" package to access the PORT number from the .env file.

At the top of the project, we're loading environment variables using dotenv.config() to make it accessible throughout the file.

5. Run Project:

In this step, we'll add a start script to the package.json file to easily run our project.

So, Add the following script to the package.json file.

"scripts": {
  "start": "node index.js"
}
Enter fullscreen mode Exit fullscreen mode

The package.json file should look like this:

Package.json

To check whether everything is working or not, let's run the project using the following command:

npm run start
Enter fullscreen mode Exit fullscreen mode

This will start the Express server. Now if we go to this URL http://localhost:8000/ we'll get this:

Localhost:8000/

Awesome! The Project setup is done and it's working perfectly. Next up, we'll add Gemini to our project in the next section

Publishing Articles to Dev.to

At First to Publsih Articles to Dev.to we have to get the API Key!

For that We'll go to https://dev.to/settings/extensions And Here we'll generate the API key!

Dev.to API Key

After getting the API Keys, We'll add them in the .env file that we have created before.

Now, We'll get the ApiKey from the .env file.

const ApiKey = process.env.API_KEY;
Enter fullscreen mode Exit fullscreen mode

Here, We have stored the API key, and URL in variables. This header will be used in the HTTP requests.

Next, we'll create a /blog route to Publish the content to Dev.to!

app.use(express.json());

app.post('/blog', async (req, res) => {
    const {article} = req.body;
    try {
        const response = await axios.post(
            "https://dev.to/api/articles",
            {
              article,
            },
            {
              headers: {
                "Content-Type": "application/json",
                "api-key": ApiKey,
              },
            }
          );
        return res.status(201).send(response.data);
    } catch (error) {
        console.error(error);
        res.status(500).send('An error occurred');
    }
})
Enter fullscreen mode Exit fullscreen mode

Here, We are taking the Article content from the Request Body and publishing the Content to Medium using the API endpoint.

With that,Our Coding Part is Done!

Testing:

Now, let's see if everything is working correctly.

We'll Open Postman and make a POST request to http://localhost:8000/blog URL. With this body :

{
  "article": {
    "body_markdown": "###  content for the body markdown. It can contain various details, sections, and formatting like headers, lists, and more.",
    "description": "A brief overviecontent and its purposevhhvhv",
    "published": true,
    "title": "Publishing Article on DevTo | by Arindam",
    "canonical_url": "https://arindam1729.hashnode.dev",
    "tags": ["DevTo", "Arindam", "Test","Blog"]
  }
}
Enter fullscreen mode Exit fullscreen mode

And we'll get something like this:

Postman Console

Awesome! We got the 201 status code that means it worked. Now Just to double check, Let's see our Dev.To Dashboard!

Demo Blog Live on Dev.To

Awesome! Our Blog is Live!

Fetching Articles from Dev.to

In this section, we'll fetch the our Dev.to blogs!

Here, We'll create a /getBlogs route where we'll make a get request to fetch the Blogs from our Dev.to Account.

app.get("/getBlogs", async(req, res) => {
    try {
        const response = await axios.get(
            "https://dev.to/api/articles/me/published",
            {
              headers: {
                "Content-Type": "application/json",
                "api-key": ApiKey,
              },
            }
          );
        return res.status(200).send(response.data);
    } catch (error) {
        console.error(error);
        res.status(500).send('An error occurred');
    }
});
Enter fullscreen mode Exit fullscreen mode

Now, let's see if everything is working correctly.

We'll Open Postman and make a GET request to http://localhost:8000/getBlogs URL. And we'll get something like this:

Postman Console-2

Awesome! We got the 200 status code that means it worked.

With that, We have done the basic implementation publishing and getting Blogs of Dev.to . In the Future Blogs, we'll do more implementations!

Till then, Stay Tuned!

Conclusion

If you found this blog post helpful, please consider sharing it with others who might benefit. You can also follow me for more content on Javascript, React, and other web Development topics.

For Paid collaboration mail me at : arindammajumder2020@gmail.com

Connect with me on Twitter, LinkedIn, Youtube and GitHub.

Thank you for Reading :)

Thank you

Top comments (0)