Simple gem to connect to Zoho CRM API V2

api, api-client, zoho
gem install zoho_hub -v 0.1.55



Gem Version

Simple wrapper around Zoho CRM version2, using OAuth 2.0 protocol for authentication.

This gem reads your Module configuration and builds the corresponding classes for you, using some reflection mechanisms. You should then be able to use simple classes with an API close to ActiveRecord, to do CRUD operations.

NOTE: this gem is WIP, please try to use it and open an issue if you run into limitations / problems

Table of Contents


Add this line to your application's Gemfile:

gem 'zoho_hub'

And then execute:

$ bundle

Or install it yourself as:

$ gem install zoho_hub

Setup process

1. Register your application

If you want to access your Zoho CRM account from your application you first need to create your application as described here:

This will give you a Client ID and a secret, that you'll use in step 2.

1.1 Zoho Accounts URL

Registration and authorization requests are made to Zoho's domain-specific Accounts URL which varies depending on your region:

ZohoHub uses the EU Account URL by default, but this can be overriden in a ZohoHub.configure block via the api_domain method (step 2.)

1.2 Authorized Redirect URI

Per Zoho's API documentation, providing a redirect URI is optional. Doing so allows a user of your application to be redirected back to your app (to the redirect URI) with a grant token upon successful authentication.

If you don't provide a redirect URI, you'll need to use the self-client option for authorization (see 3.2.)

2. Configure ZohoHub with your credentials

Note: Treat these credentials like an important password. It is strongly recommended to not paste them anywhere in plain text. Do not add them to version control; keep them out of your code directly by referencing them via environment variables. Use something like the dotenv gem or encrypted credentials in Rails to keep them as secret and secure as possible.

You need to have a configuration block like the one below (in Rails add a zoho_hub.rb in your config/initializers directory):

ZohoHub.configure do |config|
  config.client_id    = 'YOUR_ZOHO_CLIENT_ID' # obtained in 1.
  config.secret       = 'YOUR_ZOHO_SECRET'    # obtained in 1.
  config.redirect_uri = 'YOUR_ZOHO_OAUTH_REDIRECT_URL'
  config.api_domain   = '' # can be omitted if in the EU
  # config.debug      = true # this will be VERY verbose, but helps to identify bugs / problems

3. Authorization request

In order to access data in Zoho CRM you need to authorize ZohoHub to access your account. To do so you have to request a specific URL with the right scope and access_type. Successful authorization will provide a grant token which will be used to generate access and refresh tokens.

3.1 Redirection based authentication

To get the right URL you can use this simple line of code:

# => ",ZohoCRM.settings.all,ZohoCRM.modules.contacts.all,ZohoCRM.modules.all"

If you request this generated URL you should see a screen like this one, where you have to click on "Accept":

You will then be redirected to the redirect URI you provided with additional query parameters as follows (the value after code= is the grant token):


3.2 Self-Client Authorization

If you don't have a redirect URI or you want your application to be able to authorize with Zoho programmatically (without a user required to be present and click the "Accept" prompt), Zoho provides a self-client option for authentication which will provide a grant token.

3.3 More on scopes

You can change the default scope (what data can be accessed by your application). This is the list provided as the default scope:


To get the URL for a different scope you can provide a scope argument:

ZohoHub::Auth.auth_url(scopes: ['ZohoCRM.modules.custom.all', 'ZohoCRM.modules.all'])
# => ",ZohoCRM.modules.all"

Refer to Zoho's API documentation on scopes for detailed information.

3.4 Offline access

By design the access tokens returned by the OAuth flow expire after a period of time (1 hour by default), as a safety mechanism. This means that any application that wants to work with a user's data needs the user to have recently gone through the OAuth flow, aka be online.

When you request offline access the Zoho API returns a refresh token. Refresh tokens give your application the ability to request data on behalf of the user when the user is not present and in front of your application.

By default ZohoHub::Auth.auth_url will request offline access

You can force "online" access by using #auth_url with a access_type argument:

ZohoHub::Auth.auth_url(access_type: 'online')
# => ",ZohoCRM.settings.all,ZohoCRM.modules.contacts.all,ZohoCRM.modules.all"

4. Access token

See Zoho's API documentation for generating an initial access token:

To use an access token in a manual request, include it as a request header as Authorization: Zoho-oauthtoken {access_token} (without the braces.)

To use an access token with ZohoHub, pass it to the ZohoHub.setup_connection method as the access_token parameter.

5. Refresh token

This gem automatically refresh the access token.

If you want automatic refresh, use the refresh_token argument as in the next chapter.

6. Basic ZohoHub flow

Once ZohoHub has been configured with your credentials (section 2) and you have a fresh access token, setup a ZohoHub connection:

ZohoHub.setup_connection access_token: 'ACCESS_TOKEN',
                         expires_in: 'EXPIRES_IN_SEC',
                         api_domain: 'API_DOMAIN',
                         refresh_token: 'REFRESH_TOKEN'

Now you can issue requests to Zoho's API with the Connection object, e.g.:

# request a (paginated) list of all Lead records
ZohoHub.connection.get 'Leads'

A successful request will receive a response like the sample here:

7. BaseRecord and record classes

At this point, ZohoHub is starting to do some of the heavy lifting, but using ZohoHub.connection still gets tedious after just a handful of requests. But we can improve that by allowing ZohoHub to build our record classes or by manually defining them ourselves.

7.1 Reflection


7.2 Subclassing BaseRecord

See lib/zoho_hub/base_record.rb and any of the classes in examples/models/ for reference.

For any Zoho module with which you want to interact via ZohoHub, make a class of the same name that inherits from ZohoHub::BaseRecord. For example, to build a class for the Leads module:

# lead.rb

class Lead < ZohoHub::BaseRecord

Specify this module's fields as attributes:

# lead.rb

class Lead < ZohoHub::BaseRecord
  attributes :id, :first_name, :last_name, :phone, :email, :source, # etc.

Now you can issue requests more easily with your record class, e.g.:

# Request a (paginated) list of all Lead records

# Get the Lead instance with a specific ID

And even create new Lead entries in Zoho:

lead =
  first_name: 'First name',
  last_name: 'Last name',
  phone: '+35197736281',
  email: '',
  source: 'Homepage'

# Creates the new lead

# Or in one step:
lead = Lead.create(first_name: 'First name', ...)

Updating records:

Lead.update(id:, first_name: "...", last_name: "...")

# Or
lead.update(first_name: "...", last_name: "...")

# Or update up to 100 records in one call:
leads = [{ id: id1, phone: "123" }, { id: id2, first_name: "..." }]

Blueprint transition:

Lead.blueprint_transition(, transition_id)

# Or

Adding notes:

Lead.add_note(id:, title: 'Note title', content: 'Note content')

Related records:

Product.all_related(parent_module: 'Lead', parent_id:
  parent_module: 'Lead',
  parent_module: 'Lead',

Attachments (ZohoHub::Attachment is defined in the gem):

# -> Array of Attachments

attachment = Lead.download_attachment(parent_id:,
# -> Attachment (attachment.file contains the file as a Tempfile)

#NB: Lead.upload_attachment not implemented yet

8 Notifications

Zoho allows you to receive a notification when a record of a module changes. Supported operation types are create, delete, edit, all.

8.1 Enable notifications

In order to receive notifications, you have to enable them first.

# Enable notifications for a given channel:
notification_url = '' # Zoho will send notifications by POST to this url
token = '123abc' # Zoho will send this token back to you, so you can ensure that the notification is from Zoho
channel_id = 1 # Choose a channel to handle the response
events = %w[Leads.create Deals.edit Contacts.delete Sales_Orders.all] # Which events to receive notifications for
channel_expiry = ( + # choose a date when the channel should expire. 24h is the maximum, default is one hour

ZohoHub::Notifications.enable(notification_url, channel_id, events, channel_expiry, token)

After enabling notifications, Zoho will execute a POST request to the provided notification_url every time the requested event occurs.

For a list of an in-depth description of the response, check the Zoho documentation

8.2 List notifications

You can also retrieve all notifications that are currently enabled and that you are receiving uppdates for.

# Get all enabled notifications

8.3 Caveats

  • Zoho does not notify you when records are merged.
  • Since Zoho does not tell you what changed, you will have to request the record by yourself. Due to this you can miss changes, when they occur quickly after another. This is especially important for status changes, as you might miss state changes.

Tips and suggestions

  • Using a tool such as Postman or curl to issue HTTP requests and verify responses in isolation can be a great sanity check during setup.
  • Downloading ZohoHub code (as opposed to the gem) and running bin/console is a great way to learn how the code works and test aspects of setup and Zoho's API in isolation.
  • The Zoho API Documentation is your friend - especially the sample HTTP requests and responses in the various sections under "Rest API" on the left.
  • If you're manually implementing your record classes (rather than using the reflection mechanism), the files in /examples/models/ can help you get started.
  • Requests can be issued to Zoho CRM's Sandbox by configuring (or regional equivalent) as the api_domain.


Setup auth token and request CurrentUser

This example assumes use of the dotenv gem and is written directly into ZohoHub's root directory (rather than using ZohoHub as a gem) for simplicity.

  1. Edit bin/console to comment out refreshing the token and setting up the connection:
# bin/console

# puts 'Refreshing token...'
# token_params = ZohoHub::Auth.refresh_token(ENV['ZOHO_REFRESH_TOKEN'])
# ZohoHub.setup_connection(token_params)
  1. Register your application to obtain a client ID and secret. (Leave the Zoho API Credentials page open; you'll need it in step 5.)
  2. Determine your Zoho Accounts URL.
  3. Add your registration and account URL information to a .env file:
# .env

  1. On the Zoho API Credentials page from step 1, click the three vertical dots and select Self client.
  2. Paste this into the Scope field: ZohoCRM.users.ALL, choose an expiration time, and click View Code; this is your Grant token.
  3. Run the ZohoHub console from your terminal: bin/console
  4. Issue a token request with the grant token (notice the quotes around the token value):

This should return a response with an access token, e.g.:

=> {:access_token=>"ACCESS_TOKEN_VALUE",

exit the console with exit.

  1. Add the access token to your .env file:
# .env

  1. Edit bin/console to add a new setup_connection after the previously commented out one:
# bin/console

# ZohoHub.setup_connection(token_params)

ZohoHub.setup_connection(access_token: ENV['ZOHO_ACCESS_TOKEN'])
  1. Start the console again: bin/console.

  2. Issue a request for the current user: ZohoHub.connection.get 'users?type=CurrentUser'


After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install.


Bug reports and pull requests are welcome on GitHub at


The gem is available as open source under the terms of the MIT License.