snoowrap

A fully-featured JavaScript wrapper for the reddit API. (Documentation)

Features

snoowrap provides a simple interface to access every reddit API endpoint. For example, the method to get a user profile is just getUser(), and the method to upvote something is just upvote().

snoowrap is non-blocking; all of its API calls are asynchronous and return bluebird Promises. This means that you can handle concurrent requests however you want to, and you can use snoowrap as part of a larger process without it holding everything back.

Each snoowrap object is completely independent. This means that you can have scripts from separate accounts making requests at the same time.

After you provide a token once, snoowrap will refresh it on its own from then on -- you won't have to worry about authentication again.

snoowrap uses lazy objects, so it never fetches more than it needs to.

snoowrap has built-in ratelimit protection. If you hit reddit's ratelimit, you can choose to queue the request, and then run it after the current ratelimit period runs out. That way you won't lose a request if you go a bit too fast.

snoowrap will retry its request a few times if reddit returns an error due to its servers being overloaded.

These features ensure that you can write less boilerplate code and focus more on actually doing what you want to do.

snoowrap's methods are designed to be as simple and consistent as possible. So the following expression:

For more examples of what can be done with snoowrap, take a look at the documentation.

Live threads

Reddit's live threads are different from most other content, in that messages are distributed through websockets instead of a RESTful API. snoowrap supports this protocol under the hood by representing the content stream as an EventEmitter. For example, the following script will stream all livethread updates to the console as they appear:

Environments that support the ES6 Proxy object

// This works in environments that support Proxies.
// However, it throws a TypeError if Proxies are not available.
r.getSubmission('47v7tm').comments[0].upvote();
// ----------
// This is equivalent and works on all platforms, but the syntax isn't as nice.
r.getSubmission('47v7tm').fetch().then(submission => {
return submission.comments[0].upvote();
});

You can explicitly disable method chaining with r.config({proxies: false}).

These files will occasionally be updated as new versions of snoowrap 1 are released. Since snoowrap follows semantic versioning, the changes should not break your code. However, if you would prefer to pin the version, you can specify a version number in the URL (e.g. snoowrap-v1.11.3.min.js). For a list of all available prebuilt versions, see the gh-pages branch of this repository.

When run in a browser, snoowrap will be assigned to the global variable window.snoowrap. If you want to avoid global state (or you're using two versions of snoowrap on the same page for some reason), use snoowrap.noConflict() to restore window.snoowrap to its previous value.