In this article, we’ll cover the use of Python SDK with Zoho CRM. It’s a tricky topic, and many people find themselves stuck figuring it out. The documentation on the subject is notoriously confusing, so we hope to clear things up for you.
First, install the Zoho CRM SDK.
To do that, we’re using pipenv. Run
pipenv init to kick things off.
Then in your terminal, run
pipenv install zcrmsdk to install the library.
Next up we’ll activate our dependencies by running
Now you can create a file called init.py where we’ll initialise the SDK. You can see we’ve imported ZCRMRestClient, and Zoho0Auth.
Then, we’ve done a little config from lines 3-12.
- sandbox: put False here
- applicationLogFilePath: ./log should work (you’ll need to create that folder)
- client_id/client_secret: we’ll cover this later
- redirect_uri: you can put any URL here. The redirect_uri is less relevant for self_client applications (what we’ll be using) so you can put anything here (but make sure you put the same URL in the API console when you get to the next step).
- token_persistence_path: “.” is fine. You’ll need to create an empty file in the project folder called zcrm_oauthtokens.pkl which is where the auth token will be stored
- currentUserEmail: put your Zoho CRM user email
Adding The Client
Go to your developer console at accounts.zoho.com/developerconsole. Click “add new client” and you’ll be sent to the following screen:
Here, you can name your clients and domains accordingly. The highlighted portion doesn’t even matter unless you want someone to log into your app. If you’re only using Zoho CRM, a back-end server is your goal.
Once you click create. You’ll see this screen, which will give you your Client ID and Client Secret. Those go back into the config. You’ll be using a self-client because you won’t be handling oauth.
While you’re back at the config, make sure your redirect URI and current user email match what you put in
Next, click the 3 dots on the right of your new client, and select “self-client”.
For the scope, put in “AAAServer.profile.read”. Then, you’ll need to put in some other things depending on what you’re doing with the API.
In ours, we’ve also included ZohoCRM.Modules.ALL, ZohoCRM.Bulk.READ, and ZohoCRM.org.all so that we can access those API methods. You can find the scopes you need by checking the Zoho CRM API documentation:
Upon clicking “view code”, you’ll receive the grant code. You can update the grant_code variable in init.py and then run
python init.py. That will authenticate with the Zoho CRM API using the grant code, retrieve a refresh token and store the refresh token in zcrm_oauthtokens.pkl
Now we’ve overcome the OAuth hurdle, we’re ready to actually run some API methods. Create another file (name doesn’t matter – we called ours test.py). Use the same config as before, making sure to import ZCRMRecord and ZCRMRestClient. Make sure to also have the highlighted text below.
When .initialize is called, the SDK will go into the pkl file and and pull out the oauth details to get a refresh token. This means that you’re authenticated and can get data out of the CRM.
Check out the lines above from 17 to 21. We’re getting data from the lead. Where do you get this code?
Go to the CRM, then Rest API Reference>Record APIs>Get Specific Record data. Next click on Python on the right.
Once you’ve done that, it will give you some example code to use.
You should now be able to use your Python skills to query data from the Zoho API 🙂
Deploying the code to a server
Let’s say you’re deploying this code to Heroku. You would need to run init.py locally once, which will then give you the pkl file. At that point, you never need to run init.py again, because you’ve got the oauth data in a pkl file and your API handler (the one you’d deploy) can always use that to get more refresh tokens. Make sure you don’t publish the refresh token/API secret (don’t worry I revoked the tokens you can see in this article) as it gives someone access to your CRM account.
Python Microservices Without Python Zoho CRM SDK
Whew that was a lot of work. There is arguably a much easier way. Check out our article on integrating with serverless custom functions.