How to post to WordPress from Node.js (With Image)

Post to WordPress Node.js

Creating a blog post on WordPress using Node.js is easier than you might think. In this guide, we’ll walk through how to authenticate, upload a featured image, and publish a post directly to your WordPress site using the REST API.

This is perfect for developers looking to automate publishing workflows or integrate content systems with WordPress.

Set your authentication credentials

To post to WordPress, you’ll need a username and an Application Password. These credentials will allow you to authenticate API requests securely from Node.js.

WordPress 5.6 will finally see the introduction of a new system for making authenticated requests to various WordPress APIs — Application Passwords.

Getting credentials

From the Edit User page, Users -> Profile you can generate new, and view or revoke existing application passwords. The form and the list table are both fully extensible to allow for overloading to store additional data.

Post to WordPress Node.js application password
Application Passwords dashboard
					
const ourusername = "Your Username from WP"
const ourpassword = "Your Application Password Name from WP"

Use Basic Auth to authorize your requests.

Upload a featured image to WordPress

We start by reading a local image and uploading it to the WordPress Media Library.

					
const imagepath = path.join(__dirname, "cat2.jpg")
    const imagebuffer = fs.readFileSync(imagepath)

    const imagerequestheaders = new Headers()
    imagerequestheaders.set("Content-Type", "image/jpeg")
    imagerequestheaders.set("Content-Disposition", "attachment; filename=amazing-cat.jpg")
    imagerequestheaders.set("Authorization", "Basic " + Buffer.from(`${ourusername}:${ourpassword}`).toString("Base64"))

    const imagepromise = await fetch("https://node-testing.local/wp-json/wp/v2/media",{
        method: "POST",
        headers: imagerequestheaders,
        body: imagebuffer
    })

    const imageresult = await imagepromise.json()

Then, send a POST request to wp-json/wp/v2/media.

Create and publish your post to WordPress

Once you receive the media ID, use it as the featured_media in your blog post request.

					
const blogpostheaders = new Headers()
    blogpostheaders.set("Content-Type", "application/json")
    blogpostheaders.set("Authorization", "Basic " + Buffer.from(`${ourusername}:${ourpassword}`).toString("Base64"))


    fetch("https://node-testing.local/wp-json/wp/v2/posts", {
        method:"POST",
        headers:blogpostheaders,
        body: JSON.stringify({
            title:"New Cat post from Nodejs!",
            featured_media: imageresult.id,
            status:"publish",
            content:"<!-- wp:paragraph --><p>This is an example paragraph.</p><!-- /wp:paragraph -->"
        })
    })

Then, send a POST request to wp-json/wp/v2/posts.

This publishes your post instantly with the featured image set.

See the full code working

					
async function go() {
    const path = require("path")
    const fs = require("fs")

    const ourusername = "Your Username from WP"
    const ourpassword = "Your Application Password Name from WP"

    /// Start image request
    const imagepath = path.join(__dirname, "cat3.jpg")
    const imagebuffer = fs.readFileSync(imagepath)

    const imagerequestheaders = new Headers()
    imagerequestheaders.set("Content-Type", "image/jpeg")
    imagerequestheaders.set("Content-Disposition", "attachment; filename=amazing-cat1.jpg")
    imagerequestheaders.set("Authorization", "Basic " + Buffer.from(`${ourusername}:${ourpassword}`).toString("Base64"))

    const imagepromise = await fetch("https://node-testing.local/wp-json/wp/v2/media",{
        method: "POST",
        headers: imagerequestheaders,
        body: imagebuffer
    })

    const imageresult = await imagepromise.json()
    /// End image request

    const blogpostheaders = new Headers()
    blogpostheaders.set("Content-Type", "application/json")
    blogpostheaders.set("Authorization", "Basic " + Buffer.from(`${ourusername}:${ourpassword}`).toString("Base64"))


    fetch("https://node-testing.local/wp-json/wp/v2/posts", {
        method:"POST",
        headers:blogpostheaders,
        body: JSON.stringify({
            title:"New Cat post from Nodejs!",
            featured_media: imageresult.id,
            status:"publish",
            content:"<!-- wp:paragraph --><p>This is an example paragraph.</p><!-- /wp:paragraph -->"
        })
    })
}

go()

In my setup, I placed all the code in a file named index.js, which you can run from the terminal using node index. You’ll find the result of the execution below.

Post to WordPress Node.js
The result after create the post from Nodejs

Start automating WordPress publishing

If you often publish content from external sources, learning how to post to WordPress with Node.js will save you time and streamline your workflow.

Need more flexibility? You can extend this to schedule posts, add tags or categories, and even post custom block content.


Share your thoughts 😉

Leave a Reply

Your email address will not be published. Required fields are marked *