Skip to content
An API over hacker news's site
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
lib Fixed login and signup Feb 21, 2016
Rakefile added bundler Mar 24, 2013


An API over Hacker News


mechanize (>= 1.0.0)
require_all (>= 1.1.0)


gem install ruby-hackernews

then, in your script:

require 'ruby-hackernews'

before using it. If you want to include the namespace (RubyHackernews), add

include RubyHackernews

otherwise, you'll have to access the gem's classes adding the module, like this:



You can get entries on the main page with:

Entry.all       # returns the main page's entries as an array

You can provide a number of pages:

Entry.all(3)    # will return the entries on the first 3 pages

There are methods for getting specific entry types:

Entry.questions # gets the first page of questions (ask HN)
Entry.newest    # gets the first page of new links (new)      # gets the first page of job offerts (jobs)
Entry.shows     # gets the first page of shows (show HN)
Entry.new_shows # gets the first page of new shows (show HN new)

You can also get a single entry by its ID:

Entry.find(3102321) # gets that specific entry (warning: number will be 0!)

Each Entry instance has the following data:

entry = Entry.all.first # gets the top entry on the mainpage

entry.number       # the entry's position on HN   # the link's name on HN    # the actual link    # the referring site, if any # the entry's score on HN    # the submitter's user name

entry.time         # the elapsed time from submission

entry.text         # the text of the submission (ask/jobs only)
                   # NOTE: it will fetch the inner page

After you've logged in (see below) you can do the following

entry.upvote                                # votes the entry
entry.write_comment("mycomment")            # adds a comment to the entry
Entry.submit("mytitle", "myurl")            # submit a new link
Entry.submit("myquestion", "question text") # submit a new question 

user.submissions.first.comments_url         # returns the url to the current user's comments


You get an entry's comments with:


You can also get a specific comment by id with:

Comment.find("1234") # returns the comments with id 1234,
                     # and its subcomments.

Note that the method above will send a request to HN. If you just need the comments' count or url, you can instead use:




Either of which will not issue a request to HN's site.

You can also get the newest comments on HN with:

Comments.newest(3)  # gets the first 3 pages of new comments

Each Comment instance has the following data:

comment = Entry.all.first.comments.first # gets the first comment of the first entry on HN's main page             # comment's unique identifier
comment.text           # comment's body      # poster's user name on HN   # comment's score

Comments are enumerable and threaded, so you can do like:

comment[2][0]          # gets the third reply to this comment, then the first reply to the reply
comment.first          # gets the first reply to this comment do |c|  # gets all the comment replies which text contains "test"
  text ~= /test/
comment.parent         # gets the comment's parent (nil if no parent)

Once you're logged in (see below), you can do the following:


Logging in

You define a user with:

user ="username")

Then, you log in with:


Or, you can create a new use with that name:

user.signup("password") # don't abuse this!

You will be also logged in with the new user. So, no need to call user#login after user#signup.

You can log out with:


You have to log out before logging in with a different user.

You can also get the current users submission list by doing this:


This will return a new Entry instance. For example:


Will return the HN comment url of the last submitted story of that user


Get user info (comments, saved)
Change user info/settings


Something went wrong with that request. Please try again.