DEV Community

Cover image for Creating your Blog using GatsbyJS and Dev.to API
Aivan Monceller
Aivan Monceller

Posted on • Edited on

64 17

Creating your Blog using GatsbyJS and Dev.to API

TLDR: You can go to the demo on what this is all about. Nonetheless, this is really very easy even if you don't know about GatsbyJS.

I have seen a couple of Dev.to API articles here and decided to make something useful with it.

If you already have a blog, this article is also for you. I have been struggling to create some content on my own domain because I feel no one would even bother visiting my site.

At Dev.to though, I just keep coming back to write articles because I know someone would be reading it somehow.

Instead of cross-posting from my own blog to Dev.to, I thought I could do the other way around. I am introducing this very basic Dev.to Gatsby source that you could use with your Gatsby website.

GitHub logo geocine / gatsby-source-dev

Gatsby source plugin that fetches user articles from Dev.to

header
gatsby-source-dev

NPM version Build Status

A Gatsby plugin that fetches user articles from Dev.to’s /articles?username combined with /articles/${id} endpoint.

Usage

Install gatsby-source-dev in your project:

yarn add gatsby-source-dev
npm install gatsby-source-dev

Then add the plugin to your gatsby-config.js file:

{
  resolve: "gatsby-source-dev",
  options: {
    // This is your username on Dev.to
    username: ''
  }
}
Enter fullscreen mode Exit fullscreen mode

The plugin will store the Dev.to API response in Gatsby. Here's an example of a query that fetches an articles title, id, and description.

{
  allDevArticles {
    edges {
      node {
        article {
          id
          title
          description
        }
      }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

The node contains the entire response from Dev.to’s endpoint.

Pagination is not yet implemented




If you do not know what a gatsby source or have never really touched Gatsby, you don't need to worry since I created a starter.

GitHub logo geocine / gatsby-starter-devto

A GatsbyJS starter template that leverages the Dev.to API




Here are the steps how to create your own blog using this starter:

1 - Clone the github repository

    git clone git@github.com:geocine/gatsby-starter-devto.git
Enter fullscreen mode Exit fullscreen mode

2 - Open gatsby-config.js and change line 13 with your Dev.to username

    module.exports = {
      siteMetadata: {
        title: 'Gatsby + Dev.to Starter',
      },
      plugins: [
        ...
        {
          resolve: 'gatsby-source-dev',
          options: {
            // your Dev.to username
            username: 'geocine'
          },
        },
        ...
      ],
    }
Enter fullscreen mode Exit fullscreen mode

3 - Install and start

     yarn
     yarn start
Enter fullscreen mode Exit fullscreen mode

4 - Navigate to http://localhost:8000

This is how it should look like using my username geocine.

This is still a work in progress, I will be adding more features, feel free to create a pull request. I hope you like it.

If you want to learn more about gatsby, checkout this article below:

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (17)

Collapse
 
tomasforsman profile image
Tomas Forsman

So, I decided to try my hand at making a source for Gatsby, even though I'm very new to it. I saw that there were an API for Dev.to and thought, hey, I should make a source for dev.to. Starting to look for the dev.to api, found this...

Collapse
 
arcath profile image
Adam Laycock

Interesting stuff, this is why I love Gatsby!

Does dev.to have the ability to fire off a webhook when you publish (or a comment is made)? Thinking thats the best way to ensure that your site always has the latest content.

Collapse
 
geocine profile image
Aivan Monceller

That would definitely be a great feature, I hope Dev.to will consider to add support for webhooks.

Collapse
 
donvitocodes profile image
donvitocodes

docs.dev.to/api/#tag/webhooks

Is this what you're looking for? Just saw it now.

Collapse
 
levivm profile image
Levi Velázquez • Edited

I added a blog header just like dev.to

screenshot

I'm cleaning it up and setting up social icons for being parameterizable, then I will submit the PR.

Collapse
 
steelwolf180 profile image
Max Ong Zong Bao

Wow, this looks awesome very interesting concept.

Collapse
 
dylanesque profile image
Michael Caveney

Smart thinking! I really prefer the experience that Dev.to creates for bloggers and their audience, but I want my entries on my own site as well. I was going to spin up cross-posting in the next iteration of my site, but this is a lot better!

Collapse
 
vekzdran profile image
Vedran Mandić

Excellent share and tool! Can you pull over comments too? Planning in building a site with gatsby and the cross post issue would defo solved with this approach. Thank you for building it.

Collapse
 
geocine profile image
Aivan Monceller

I will look into the API and check if it provides such functionality.

Collapse
 
philnash profile image
Phil Nash

This is a great idea. Hosting your stuff on your own site makes a lot of sense, but if you're going to write and publish on dev.to, then why not use it as your CMS too.

Fantastic use for Gatsby!

Collapse
 
quinncuatro profile image
Henry Quinn

Have any more info on DEV's API?

Collapse
 
peterwitham profile image
Peter Witham

Brilliant! I was thinking about this over the weekend so I'm glad I found your post to get me thinking more seriously about this.

Collapse
 
zeptobook profile image
ZeptoBook

Nice post. I also wrote a post more in details about how to create your first blog site using Gatsby. zeptobook.com/create-your-blog-sit...

Heroku

Tired of jumping between terminals, dashboards, and code?

Check out this demo showcasing how tools like Cursor can connect to Heroku through the MCP, letting you trigger actions like deployments, scaling, or provisioning—all without leaving your editor.

Learn More

👋 Kindness is contagious

Dive into this thoughtful piece, beloved in the supportive DEV Community. Coders of every background are invited to share and elevate our collective know-how.

A sincere "thank you" can brighten someone's day—leave your appreciation below!

On DEV, sharing knowledge smooths our journey and tightens our community bonds. Enjoyed this? A quick thank you to the author is hugely appreciated.

Okay