Reddit-Wrapper-V2 - Reddit API framework for Nodejs
Simple to use
Reddit-Wrapper is designed to be a simple to user reddit API wrapper, while also providing robust error handling and retry capabilities. Every function returns a promise. Allowing the user to easily handle errors in the catch, and results in the then.
reddit.api.get("/subreddits/mine/subscriber", {
limit: 2,
})
.then(function(response) {
let responseCode = response[0];
let responseData = response[1];
console.log("Received response (" + responseCode + "): ", responseData);
})
.catch(function(err) {
});
Table of Contents
Setup and Configuration
Installing reddit-snooper
npm install reddit-wrapper --save
Library usage and configuration
var RedditAPI = require('reddit-wrapper');
var redditConn = new RedditAPI(
{
username: 'reddit_username',
password: 'reddit password',
app_id: 'reddit api app id',
api_secret: 'reddit api secret',
user_agent: 'user agent for your bot',
retry_on_wait: true,
retry_on_server_error: 5,
retry_delay: 1,
})
API setup
All you need to get up and running is obtain an api_id and an api_secret. Both can be created on the Reddit app console
- Create (or log into) a reddit account
- Navigate to the authorized applications console
- Select 'create another app...' at the bottom
- Fill in the name, description and click on 'script', put in anything for the redirect uri, its not needed and you can change it later if you want to
- Copy down the 'secret' that is your api_secret, the 14 character string by the name of your app is your app_id
- Use these values and your credentials to configure the snooper
Reddit API (api)
Reddit Wrappers api component is an agnostic wrapper around Reddit's rest API that handles retries, and Reddit's different response codes.
In order to use the api head over to the Reddit API Documentation. All of the api methods use one of the 5 HTTP methods (GET, POST, PATCH, PUT, DELETE) which map to the 5 different snooper.api methods.
redditWrapper.api.get(endpoint, data)
.then(function(response) {
let responseCode = response[0];
let responseData = response[1];
console.log("Received response (" + responseCode + "): ", responseData);
})
.catch(function(err) {
return console.error("api request failed: " + err)
});
redditWrapper.api.post(endpoint, data)
.then(function(response) {})
.catch(function(err) {})
redditWrapper.api.patch(endpoint, data)
.then(function(response) {})
.catch(function(err) {})
redditWrapper.api.put(endpoint, data)
.then(function(response) {})
.catch(function(err) {})
redditWrapper.api.delete(endpoint, data)
.then(function(response) {})
.catch(function(err) {})
redditWrapper.api.get_token()
.then(function(token) {})
.catch(function(err) {})
Note: new accounts get little to no posting privileges (1 comment or post per 5 minutes or more) if you dont have any karma. If you just want to play around with the api I recommend using an active account.
Note: the response from any HTTP methods will be an array containing [responseCode, data].
basic api usage
check how much karma your bot has
redditWrapper.api.get('api/v1/me/karma', {})
.then(function(resp) {
let responseCode = response[0];
let responseData = response[1];
console.log("I have " + responseData.karma + " karma")
})
.catch(function(err) {
console.log("Error getting karma: ", err);
})
post a comment
redditWrapper.api.post("/api/comment", {
api_type: "json",
text: "Hello World"
thing_id: comment.data.name
})
.then(function(resp) {
let responseCode = response[0];
let responseData = response[1];
console.log("Posted comment!");
})
.catch(function(err) {
console.log("Error posting comment: ", err);
})
Final Notes
- This was largely based on the reddit-snooper library. However that is no longer maintained.
- This relies on Bluebird promises.
- Feel free to make feature requests, bugs, etc. I will try to be as prompt at fixing/getting back to you.