Node.js

Free Node.js Reddit bot hosting.

Reddit's API is OAuth and JSON, and Node has fetch built in. Upload your code and your polling loop runs around the clock.

Libraries

Libraries that run here

Plain fetch

Our honest recommendation. Get a token from the OAuth endpoint, poll the listings you care about, and read the rate-limit headers Reddit returns. It is less code than it sounds and there is nothing between you and the API to go stale.

snoowrap

The library most Node Reddit tutorials use. It is no longer maintained, so it still works for a lot of bots and will not be fixed for the ones it stops working for.

Deploying

Getting it running

  1. 1

    Upload your project

    Drag the folder into the file manager or push it over SFTP. Leave node_modules behind: it is installed for you on deploy.

  2. 2

    Dependencies install themselves

    Your package.json is read and installed before the bot starts. A fetch-based Reddit bot may have no dependencies at all.

  3. 3

    Point us at your entry file

    Usually index.js. Client ID, secret, username and password are read from process.env, so none of them sit in a file.

Memory

What actually fits in 256 MB

Far more than a Reddit bot needs. Node's baseline is small, a listing response is a few kilobytes of JSON, and between polls your process is idle.

The thing that grows is the Set of IDs you have already handled, which gains an entry for every post the bot ever sees. Keep it in a file on your 512 MB of disk instead: it stops the growth and it stops the duplicate replies after a restart, which is the more important of the two.

FAQ

Questions

Should I use snoowrap?

If you already have a working bot on it, there is no need to rewrite it today. For a new bot, calling the API with fetch avoids depending on a library that no longer receives fixes, and the part of the API a typical bot uses is small.

How do I handle the rate limit myself?

Reddit returns how many requests you have left and when the window resets in the response headers. Read them after each call and wait when you are close to zero. That is all the libraries do.

How do I refresh the access token?

A script-type app gets a token from your credentials, and the token lasts about a day. Request a new one when a call returns 401 or before the expiry the token response gave you. There is no refresh token to store.

Why is my bot answering old posts after a restart?

Because the IDs it had handled were in memory. Write them to disk before acting on each post, and check the file first.

Do I need to upload node_modules?

No. Upload your source and your package.json and the dependencies install on deploy.