Something I Learned This Week: The dev.to API
Tags: api, programming, python, webdev
Published: 2023-10-27 18:25:06

Around this time last year, I wrote an article about how to use Medium's (now deprecated) API. At the time, I was trying to solve the problem of working with multiple blog editors by creating a single editor that had the ability to publish articles to different blogging sites (Medium, Hackernoon, etc.). The end result worked out pretty well and I actually still use it today. Just recently, I've been wanting to branch out a little and start publishing articles on the bloggig site dev.to. Dev.to has a lot of really good articles and a pretty large developer community. Not only that, it has an API with a ton of endpoints! So, today, we are going to take a closer look at how to use the dev.to API. Let's get coding!


## Calling the API

To start off, we will be using Python to make our API calls. In a new file, import the requests module.

```
import requests

```
After that, declare a new function named publish_to_dev that takes a single parameter. That parameter will be a dictionary object containing all the needed article data.

```
def publish_to_dev(article_data):
```

Inside the body of this new function, a header object is needed so that we have a place to store a dev.to API key when making API requests.

```
header = {
  "api-key": "<API_KEY>"
}
```

To get a key, all you have to do is simply create an account on dev.to, navigate to the user settings page (found on the user dropdown menu), click on Extensions, scroll down to DEV Community API Keys and click the Generate API Key button.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uv0r7nmjix7g5h1okte7.png)

Jumping back to the code, with a newly generated key, we next need to create an article dictionary to store all of the blog article information. A few noteworthy things to point out is the body of the article needs to be in markdown format (not HTML :( ) and the tags field has to be a list.

```
data = {
  "article": {
    "title": article_data["ArticleName"],
    "body_markdown": article_data["ArticleContent"],
    "description": article_data["ArticleDescription"],
    "tags": article_data["ArticleTags"]
  }
}
```

The last thing to do in this function is POST the request. The article dictionary we just created has to be passed as a JSON object and the header object containing an API key needs passed in too.

```
dev_response = requests.post("https://dev.to/api/articles", json=article, headers=header)

print(dev_response)
```

For now, we will just print out the response from the request. However, you will want to add in some error handling should something go wrong. I'll let you handle that part :).


The second function we need for this script is Python's main function. This is where we will create some fake data and pass it to the publish_to_dev function that will (hopefully) post it to dev.to.

```
if __name__ == "__main__":
    article_data = {
        "ArticleName": "Test Article",
        "ArticleContent": '''# Test Article 
        Blah blah blah''',
        "ArticleTags": "Python, programming, testing"
    }

    publish_to_dev(article_data)
```

Once that is added in, go ahead and run the script. If you are using Linux, that can be done with this command.

```
python <YOUR_FILENAME>.py
```

If you get output similar to the following, then the test article successfully made it to dev.to.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dndrif32sd45rmu62o0x.png)

You can double check that by viewing the article on the dev.to website.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7bswe45cgoyoqhawosfs.png)

## Conclusion

Publishing an article with the dev.to API is super easy and a great tool to help automate publishing for blogs. Beyond what we covered today, the API has tons of more endpoints that could be useful to the developer too that allow for getting data such as comments, podcasts, tags, users, followers, articles, and data related to individual articles. I know I have tons of ideas on how to use this new found information to expand my blog writing application and I hope that it has inspired you to write something awesome! Feel free to leave a comment about your thoughts on the dev.to API and if you have any plans on using it, because I would love to read them. Until next time, cheers!

## References

https://realpython.com/python-main-function/#a-basic-python-main

https://developers.forem.com/api/v1#tag/articles/operation/createArticle