Skip to content

Latest commit

 

History

History

dev.to

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
title
How I post to dev.to

How I post to dev.to

  1. Set a shell variable to a suitable API key:

    API_KEY=░░░░░░░░░░░░░░░░░░░░░░░░
  2. Either (a) create a new article or (b) choose an unpublished one to amend.

    (a) Create a new article:

    https POST dev.to/api/articles api-key:$API_KEY \
      article:='{"title": "hello", "body_markdown": "world"}'

    (b) List unpublished articles:

    https dev.to/api/articles/me/unpublished api-key:$API_KEY

    To make publishing easier, each post in this repository is a directory corresponding to that ID.

  3. Move to that directory and then upload the article text:

    python3 ../article.py index.md |
    https -v PUT dev.to/api/articles/${PWD##*/} api-key:$API_KEY

    The response to this request is the only way to retrieve the article from the API before it is published.

Appendix: source code

The contents of article.py are below. This reads the markdown file and outputs a suitable JSON object:

from sys import argv
from json import dumps

with open(argv[-1]) as file_:
    output = {"body_markdown": file_.read()}
print(dumps({"article": output}))

I wrote a set of tests for this code.

Checklist

  • Cover image
  • Four tags
  • Proof read
  • Set published: true
  • Push to GitHub

References