Reddit.js is a browser based wrapper on most of the read-only Reddit API. It makes CORS requests to the Reddit API. Thus only unauthenicated (logged-out) requests are allowed.
Latest versions of Chrome, Firefox, Internet Explorer and Safari are supported.
Reddit.js is a lightweight dependency-free library which comes with minified source.
- Install via
npm install reddit.js
- Or include a link from the jsdelivr CDN.
- Or download the
reddit.min.js
file from here.
Include the minfied source via script
tag like so
<script src="https://cdn.jsdelivr.net/npm/[email protected]/reddit.min.js"></script>
The reddit
variable is exported which can be used to talk to the Reddit API. The examples
directory includes usage with pure DOM API. More sophisticated examples are on the way!
// Fetch the 5 hottest posts on /r/awww
reddit.hot('aww').limit(5).fetch(function(res) {
// res contains JSON parsed response from Reddit
console.log(res);
});
// Errors (if any) will be passed to the 2nd argument of fetch()
reddit.hot('aww').limit(5).fetch(function(res) {}, function(err) {
// err contains the error from Reddit
});
// All Reddit API filters are supported and chainable
reddit.hot('aww').limit(1).after("t3_23jf8n").fetch(function(res) {
// res contains JSON parsed response from Reddit
console.log(res);
});
// Subreddits may be omitted
// Fetch the hottest posts from Reddit
reddit.hot().fetch(function(res) {
console.log(res);
});
Fetch the hottest posts on Reddit or on a subreddit. All filters such as after
, before
, limit
are supported.
reddit.hot('aww').limit(1).after("t3_23jf8n").fetch(function(res) {
// res contains JSON parsed response from Reddit
console.log(res);
});
Fetch the newest posts on Reddit or on a subreddit. Again all filters are supported.
reddit.new('programming').before("t3_23jf8n").fetch(function(res) {
// res contains JSON parsed response from Reddit
console.log(res);
});
Fetch the top posts on Reddit or on a subreddit. As usual all filters are supported. The t
filter allows filtering by top posts of the day, week, month, year or all-time.
// Fetch the top 5 funniest posts of all time from /r/funny
reddit.top('funny').t('all').limit(5).fetch(function(res) {
console.log(res);
});
Fetch the top posts on Reddit or on a subreddit As usual all filters are supported.. The t
filter allows filtering by top posts of the day, week, month, year or all-time.
// Fetch the 25 most controversial posts from this month
reddit.controversial().t('month').limit(25).fetch(function(res) {
console.log(res);
});
Fetch comments on a link. As usual all filters are supported. Context and depth are useful filters to narrow or highlight comments.
// Fetch the hottest comment on article 23ha0a from /r/pics
reddit.comments("23ha0a", "pics").limit(1).sort("hot").fetch(function(res) {
console.log(res);
});
Search everything. All filters including syntax
and t
are supported.
reddit.search("programming").t('month').limit(1).sort("hot").fetch();
Search subreddits by title and description. Filter away.
// Fetch 5 subreddits related to gardening
reddit.searchSubreddits("gardening").limit(5).fetch();
Fetch information about Reddit or a subreddit.
// Fetch information about /r/pics
reddit.about("pics").fetch(function(res) {
console.log(res);
});
The Serendipity button.
// Fetch random articles from /r/funny
reddit.random("funny").fetch(function(res) {
console.log(res);
});
Fetch a link by fullname or a list of links by URL. Can be filtered by a subreddit.
// Fetch info about reddit itself
reddit.info().url("https://www.reddit.com/").fetch(function(res) {
console.log(res);
});
Fetch a list of recommended subreddits based on the comma separated list of subreddits supplied. Can be filtered by the omit
param.
reddit.recommendedSubreddits("programming").omit("leapmotion,CFD").fetch(function(res) {
console.log(res);
});
Fetch list of subreddits that are relevant to a search query.
reddit.subredditsByTopic("programming").fetch();
Fetch subreddits ordered by activity.
// Fetch the 25 most popular subreddits
reddit.popularSubreddits().limit(25).fetch();
Fetch newest subreddits.
// Fetch the 25 newest subreddits.
reddit.newSubreddits().limit(25).fetch();
Fetch information about the user's activity. Supported second parameters are about
, overview
, submitted
, comments
, upvoted
, downvoted
, hidden
, saved
, and gilded
. Filters such as show
, sort
, t
, type
, username
, after
, before
, count
, limit
, and sr_detail
are supported. Second parameter is optional.
reddit.user('chromakode', 'comments').limit(1).fetch(function(res) {
// res contains JSON parsed response from Reddit
console.log(res);
});
Pull requests are welcome. Reddit.js is built using node
and npm
. Once you have them installed you can run all tests with:
$ npm install
$ npm test
To build the minified source
$ npm run minify
The MIT License (MIT)
Copyright (c) 2014 Sahil Muthoo
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.