Creating a Tumblr Bot With Node.js

Have a Twitter bot that’s about to be shut down because you refuse to give money to Elon Musk’s smoldering wreckage of a social media site? Me too. I decided to move my bot to Tumblr, which feels like a much better fit all around. I created Emerald Shard Hint bot as one of my first coding projects, so it felt like an appropriate testing ground. I couldn’t find any recent guides on how to do this, so I pieced together the documentation and spun up this post that will hopefully help.

I’m writing the guide for node.js because that’s what I wrote my original bot with and I’m too lazy to learn Python or something else at the moment, but it’s important to note that the Tumblr API documentation provides lots of other options to use your library of choice.

What You Need

I’m just going to go over what’s needed to create a bot that automatically posts to a Tumblr blog, but I recommend checking out the API documentation to find out what you can do with it. You can also take a look at my bot’s repo if you want any more examples of how I put it all together.

To get started, you’ll need:

  • A shiny new Tumblr blog to post to
  • To register your application with Tumblr to get API access
  • Node.js
  • A server to run your bot, I use Linode.

Registering Your Application

The first thing we need to do is register an app with Tumblr so we can get our API keys that will give us access to make requests. At this point if you haven’t created a new blog to use for the bot, now’s the time to do so.

When you’re set, head to head to the Tumblr app management page and select Register Application. You’ll see a sign up page that looks like this:

You’ll have to fill out all of the required fields. For application website, I just put my personal blog, and for Default Callback URL and OAuth2 redirect URLs, I put the URL of the Tumblr blog for the bot.

Once that’s done, you’ll see your application at the top of the page. You’ll see your OAuth consumer key and can click show secret key to see your secret key. If you choose Explore API it should take you to a dashboard that will give you some more information, including your token info. It should look like this, formatted depending on the framework you’re using:

/code

// Authenticate via OAuth
const tumblr = require('tumblr.js');
const client = tumblr.createClient({
  consumer_key: 'XXXXX',
  consumer_secret: 'XXXXX',
  token: 'XXXXX',
  token_secret: 'XXXXX'
});

Remember this page, we’ll be using that code in a bit.

Setting Up Your Bot

Next, it’s time to create the project itself:

  • Create a new folder and spin up whatever text editor you like to use
  • Make sure node and npm is installed
  • Create a new .js file called index.js or whatever you want your main file to be.
  • Init the node project with npm init and follow the steps generate your package.json.

Since we’re working with a node.js project, we now need to install tumblr.js. Run:

npm install --save tumblr.js

That will install tumblr.js and its dependencies that we’ll need to use the API in our project. From here, we can put the pieces together. In your main .js file, paste the code from earlier:

const tumblr = require('tumblr.js');
const client = tumblr.createClient({
  consumer_key: '<consumer key>',
  consumer_secret: '<consumer secret>',
  token: '<oauth token>',
  token_secret: '<oauth token secret>'
});

Later, you might want to move the keys into their own config.js file, but for playing around it’s fine to leave it in index.js.

Now we can start working with the Tumblr API to make our blog do things. For starters, I recommend becoming best friends with the tumblr.js documentation. It’s what I’ll be referencing most of my examples from.

A note on secrets, tokens, and keys: It’s best practice, especially if you’re going to be posting your code to a public repository, to make sure you do not commit your authentication secrets. For node projects, I use a .env file to store my keys and make sure that file is added to my .gitignore file so it’s not committed. This post has a decent overview if you need more info.

Using Tumblr.js

To make sure things are working correctly, let’s make a simple request. Add this to your main index.js file:

// Show user's blog names
client.userInfo(function(err, data) {
  data.user.blogs.forEach(function(blog) {
    console.log(blog.name);
  });
});

After saving, you can run your project from the terminal with:

node index.js

(or whatever your main .js file is called)

You should see a list of blogs associated with your account displayed in your terminal. Now we’re cooking! Let’s do something more interesting.

Make a Post

To make a post, you can use the createPost method like so:

client.createPost(
	<BlogIdentifier>,
	{
		"title": "Test",
		"body": "Hell World",
		"tags": "test,bot post"
	},
	function ( err, data, response ) {
		if ( err ){
			console.log( 'error:', err );
		}
		else{
			console.log( 'post sent!' );
		}
	}
)

The createPost method takes three parameters:

  • <BlogIdentifier> is whatever blog name or URL that you want.
  • Then you pass an object with the parameters for the post you want. In the example above, I provided the title, body, and tags (a comma separated list for multiple tags). You can omit any of these fields if you want. The body parameter will accept HTML as well.
  • Callback function – code to run after the request is complete. Here I’m just adding some logging in case it errors out and a success message if it works.

Use node index.js again to run your file and check your blog to see your post!

A note on blog identifiers: Since this takes the blog name or URL, if you change your blog name after deploying your code, things will break. According to the API docs, each Tumblr post has a unique blog ID as well that you can obtain with the blogInfo method. However, it seems createPost only accepts the blog name or URL – passing the UUID didn’t work for me. I’ll update this post if I figure anything out.

That’s pretty much the gist of it! From here I recommend exploring the documentation to see what other methods you can use to create the bot you want. For my bot, I just have it set to create a new post at a random time within 24 hours that pulls from an array of Sonic Adventure 2 quotes.

Now What?

I recommend checking out the full API docs as well to see what else you can do with it. For example, the createPost method seems to only create a post using HTML. I tried using postRequest to create a post with the new post format, but again didn’t have any luck with that. I’ll update this post if I figure out how that works.

Deploying the App

So your bot runs on your computer every time you manually spin up node, but you likely want your bot to live on a dedicated server so you don’t have to, ya know, keep your computer on all the time so your meme bot can live.

For me, I found Linode was an easy choice – they had clear, fair pricing and everything is well documented. Once you move your project onto the server and have everything you need installed, I also recommend using PM2 to run the application. This will ensure your bot stays online and has some handy logging so you can keep an eye on things.

Feel free to leave any comments with questions or advise

Some Things Written About

Featured Tumblr Post

Email Subscriptions