Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
205 lines (137 sloc) 6.43 KB



crocodoc-ruby is a Ruby wrapper for the Crocodoc API. The Crocodoc API lets you upload documents and then generate secure and customized viewing sessions for them. Our API is based on REST principles and generally returns JSON encoded responses, and in Ruby are converted to hashes unless otherwise noted.


We suggest installing the library as a gem.

gem install crocodoc

You can also add the library as a submodule in your git project.

git submodule add

You can also get the library by cloning or downloading.

To clone:

git clone

To download:

wget -O
mv crocodoc-crocodoc-ruby-* crocodoc-ruby

Require the library into any of your Ruby files.

If you have the gem installed:

require 'crocodoc'

If you have the files locally:

require_relative /path/to/crocodoc-ruby/crocodoc.rb

Please note that this library also requires that the 'rest-client' gem is installed, and a version of Ruby >= 1.9.

Getting Started

You can see a number of examples on how to use this library in examples.rb. These examples are interactive and you can run this file to see crocodoc-ruby in action.

To run these examples, open up examples.rb and change this line to show your API token:

Crocodoc.api_token = 'YOUR_API_TOKEN'

Save the file, make sure the example-files directory is writeable, and then run examples.rb:

ruby examples.rb

You should see 15 examples run with output in your terminal. You can inspect the examples.rb code to see each API call being used.

To start using crocodoc-ruby in your code, set your API token:

Crocodoc.api_token = 'YOUR_API_TOKEN'

And now you can start using the methods in Crocodoc::Document, Crocodoc::Download, and Crocodoc::Session.

Read on to find out more how to use crocodoc-ruby. You can also find more detailed information about our API here:

Using the Crocodoc API Library


Errors are handled by throwing exceptions. We throw instances of CrocodocError.

Note that any Crocodoc API call can throw an exception. When making API calls, put them in a begin/rescue block. You can see examples.rb to see working code for each method using begin/rescue blocks.


These methods allow you to upload, check the status of, and delete documents.

To upload a document, use Crocodoc::Document.upload(). Pass in a url (as a string) or a file resource object. This function returns a UUID of the file.

// with a url
uuid = Crocodoc::Document.upload(url)

// with a file
file_handle =, 'r')
uuid = Crocodoc::Document.upload(file_handle)

To check the status of one or more documents, use Crocodoc::Document.status(). Pass in the UUID of the file or an array of UUIDS you want to check the status of. This function returns a hash containing a "status" string" and a "viewable" boolean. If you passed in an array instead of a string, this function returns an array of hashes containing the status for each file.

// status contains status['status'] and status['viewable']
status = Crocodoc::Document.status(uuid)

// statuses contains an array of status hashes
statuses = Crocodoc::Document.status([uuid, uuid2])

To delete a document, use Crocodoc::Document.delete(). Pass in the UUID of the file you want to delete. This function returns a boolean of whether the document was successfully deleted or not.

deleted  = Crocodoc::Document.delete(uuid)


These methods allow you to download documents from Crocodoc in different ways. You can download originals, PDFs, extracted text, and thumbnails.

To download a document, use Crocodoc::Download.document(). Pass in the uuid, an optional boolean of whether or not the file should be downloaded as a PDF, an optional boolean of whether or not the file should be annotated, and an optional filter string. This function returns the file contents as a string, which you probably want to save to a file.

// with no optional arguments
file = Crocodoc::Download.document(uuid)

// with all optional arguments
file = Crocodoc::Download.document(uuid, true, true, 'all')

To download a thumbnail, use Crocodoc::Download.thumbnail(). Pass in the uuid and optionally the width and height. This function returns the file contents as a string, which you probably want to save to a file.

// with no optional size arguments
thumbnail = Crocodoc::Download.thumbnail(uuid)

// with optional size arguments (width 77, height 100)
thumbnail = Crocodoc::Download.thumbnail(uuid, 77, 100)

To download extracted text from a document, use Crocodoc::Download.text(). Pass in the uuid. This function returns the extracted text as a string.

text = Crocodoc::Download.text(uuid)


The session method allows you to create a session for viewing documents in a secure manner.

To get a session key, use Crocodoc::Session.create(). Pass in the uuid and optionally a params hash. The params hash can contain an "is_editable" boolean, a "user" hash with "id" and "name" fields, a "filter" string, a "sidebar" string, and booleans for "is_admin", "is_downloadable", "is_copyprotected", and "is_demo". This function returns a session key.

// without optional params
session_key = Crocodoc::Session.create(uuid)

// with optional params
session_key = Crocodoc::Session.create(uuid, {
    'is_editable' => true,
    'user' => {
        'id' => 1,
        'name' => 'John Crocodile'
    'filter' => 'all',
    'is_admin' => true,
    'is_downloadable' => true,
    'is_copyprotected' => false,
    'is_demo' => false,
    'sidebar' => 'visible'


Please use github's issue tracker for API library support.