Logo

dev-resources.site

for different kinds of informations.

Cross-Posting to Dev.to with API

Published at
8/20/2024
Categories
devto
nix
api
Author
vst
Categories
3 categories in total
devto
open
nix
open
api
open
Author
3 person written this
vst
open
Cross-Posting to Dev.to with API

Let's cross-post this blog post to Dev.to using its API.

Getting Started

We will use the Dev.to API (Version 1) to create a new article on Dev.to. The API requires an API key to authenticate the user. You can get the API key from the Dev.to settings page.

If you have obtained the API key, you can use it to authenticate the API requests via the api-key header. Let's give it a try:

$ curl -sH "api-key: MyVerySecretApiKey" https://dev.to/api/users/me | jq .
{
  "type_of": "user",
  "id": 1431,
  "username": "username480",
  "name": "Willy \"Myron\" \\:/ Herzog",
  "twitter_username": "twitter480",
  "github_username": "github480",
  "summary": null,
  "location": null,
  "website_url": null,
  "joined_at": "Apr 14, 2023",
  "profile_image": "/uploads/user/profile_image/1431/b547e3a6-5076-44dd-a4f6-9b85022b4e76.jpeg"
}
Enter fullscreen mode Exit fullscreen mode

OK, we are ready. Let's create a new article on Dev.to.

Create a New Article

To create a new article, we need to send a POST request to the /articles endpoint. The payload seems to be a JSON object with the following structure:

{
  "article": {
    "title": "string",
    "body_markdown": "string",
    "published": false,
    "series": "string",
    "main_image": "string",
    "canonical_url": "string",
    "description": "string",
    "tags": "string",
    "organization_id": 0
  }
}
Enter fullscreen mode Exit fullscreen mode

The tags field is documented to be a string, but it seems to be an array of strings. Let's do a dry run (published: false):

curl https://dev.to/api/articles \
    -s \
    -H "api-key: MyVerySecretApiKey" \
    -H "content-type: application/json" \
    -X POST \
    -d '{
  "article": {
    "title": "Cross-Posting to Dev.to with API",
    "body_markdown": "Example markdown content",
    "published": false,
    "tags": ["devto", "technical"],
    "canonical_url": "https://example.com/path/to/article"
  }
}'
Enter fullscreen mode Exit fullscreen mode

It worked on my computer!

Scripting?

I have written a small script to automate the process. It consumes the path to the markdown file as an argument and creates a draft article on Dev.to. I am not setting tags as I would like to do it manually as per available tags on Dev.to.

#!/usr/bin/env bash

## Get the path to the Markdown file:
_path="${1}"

## Extract the title from the front-matter of the Markdown file:
_title="$(yq --front-matter=extract .title "${_path}")"

## Convert the Markdown file to target format (commonmark):
_body="$(dev-md-format "${_path}")"

## Get the filename of the Markdown file:
_filename=$(basename -- "${_path}")

## Extract the slug from the filename:
_slug="$(echo "${_filename%.*}" | cut -f 2- -d "_")"

## Build the URL for the post:
_url="https://thenegation.com/posts/${_slug}/"

## Build the payload for the API request:
_payload="$(jq \
  --null-input \
  --arg title "${_title}" \
  --arg body "${_body}" \
  --arg url "${_url}" \
  '{
    "article": {
      "title": $title,
      "body_markdown": $body,
      "published": false,
      "canonical_url": $url
    }
  }'
)"

## Make the API request:
curl \
  -sX POST \
  -H "api-key: ${THENEGATION_DEVTO_APIKEY}" \
  -H "content-type: application/json" \
  -d "${_payload}" \
  "https://dev.to/api/articles" |
  jq -r .url
Enter fullscreen mode Exit fullscreen mode

For my convenience:

  1. I added this script to my Nix shell with name dev-cross-post-devto.
  2. I updated my .envrc file to export the THENEGATION_DEVTO_APIKEY variable.

So, now I can cross-post to Dev.to with a single command:

dev-cross-post-devto content/posts/2024-08-20_crosspost-api-devto.md
Enter fullscreen mode Exit fullscreen mode
nix Article's
30 articles in total
Favicon
Easy development environments with Nix and Nix flakes!
Favicon
A Conversation with Docker CTO Justin Cormack and Flux CEO Ron Efrani: The Future of Developer Environments
Favicon
Nice one
Favicon
NixOS - A Unique Linux Distribution
Favicon
Getting started with Nix and Nix Flakes
Favicon
My new Nix series!
Favicon
gRPC, Haskell, Nix, love, hate
Favicon
Easy way to setup Flutter Development Environment on NixOS without Flakes or dev-shell
Favicon
Dotfiles, the nix way
Favicon
Why Use Nix package manager, Even on macOS?
Favicon
Easy GitHub CLI Extensions with Nix
Favicon
Abusing Haskell: Executable Blog Posts
Favicon
Nix first steps
Favicon
Cross-Posting to Dev.to with API
Favicon
An Introduction to Nix for Ruby Developers
Favicon
Using niv to Manage Haskell Dependencies
Favicon
Don't Rebuild Yourself - an Intro to Nix Package Caches
Favicon
Packing Custom Fonts for NixOS
Favicon
Embrace the Power of Nix for Your Python + Rust Workflow
Favicon
How to Deploy Flutter on Upsun
Favicon
Declarative and reproducible environments with colima, nix and k8s
Favicon
Combining Nix with Terraform for better DevOps
Favicon
The Perfect System Configuration
Favicon
Azure Function app that runs Haskell Nix package
Favicon
A Journey to Find an Ultimate Development Environment
Favicon
The one thing I do not like about the Nix package manager (and a fix for it)
Favicon
Creating Repeatable Builds
Favicon
Develop R Packages under Nix Shell
Favicon
How I use Nix in my Elm projects
Favicon
Nix Quick Tips - Flake for OCaml

Featured ones: