Zoho Reports API
AnalyticsGetting Started with Zoho Analytics API
Are you looking for a way to access your Zoho Analytics data programmatically? Well, you're in luck! Zoho Analytics provides an easy-to-use API that allows you to access your data and perform operations using any programming language.
Authentication
Before making any API requests, you'll need to authenticate using OAuth 2.0. You'll need to create an OAuth client in your Zoho Analytics account and obtain an access token.
const CLIENT_ID = 'your_client_id';
const CLIENT_SECRET = 'your_client_secret';
const REDIRECT_URI = 'https://yourapp.com/callback';
const SCOPES = 'ZohoAnalytics.reports.READ,ZohoAnalytics.reports.CREATE';
const authUrl = `https://accounts.zoho.com/oauth/v2/auth?client_id=${CLIENT_ID}&redirect_uri=${REDIRECT_URI}&response_type=code&access_type=offline&scope=${SCOPES}`;
// Redirect user to authorization URL
window.location.href = authUrl;
Once the user grants access, you'll receive an authorization code that you can exchange for an access token.
const request = require('request-promise-native');
const CODE = 'authorization_code';
const accessUrl = 'https://accounts.zoho.com/oauth/v2/token';
const accessOptions = {
method: 'POST',
uri: accessUrl,
formData: {
grant_type: 'authorization_code',
client_id: CLIENT_ID,
client_secret: CLIENT_SECRET,
redirect_uri: REDIRECT_URI,
code: CODE,
},
json: true,
};
const { access_token, refresh_token } = await request(accessOptions);
You should store the access token and refresh token securely, as you'll need them for subsequent API requests.
Retrieving Reports
Now that you're authenticated, you can start retrieving reports from your Zoho Analytics account.
const reportUrl = 'https://analytics.zoho.com/api/v1/reports?api_key=your_api_key';
const reportOptions = {
method: 'GET',
uri: reportUrl,
headers: {
'Authorization': `Zoho-oauthtoken ${access_token}`,
},
json: true,
};
const reports = await request(reportOptions);
This will return a list of reports in your Zoho Analytics account.
Creating Reports
You can also create reports programmatically using the API. Here's an example of how you can create a pivot table report using Javascript.
const createUrl = 'https://analytics.zoho.com/api/v1/reports';
const createOptions = {
method: 'POST',
uri: createUrl,
headers: {
'Authorization': `Zoho-oauthtoken ${access_token}`,
'Content-Type': 'application/json',
},
body: {
"TemplateName": "pivot_table",
"PivotTable": {
"name": "My Pivot Table",
"subjectArea": "Orders",
"format": "json",
"filters": [
{
"column": {
"name": "Order_Status"
},
"operator": "equals",
"value": "Shipped"
}
],
"rows": [
{
"name": "Country"
}
],
"columns": [
{
"name": "Year"
}
],
"values": [
{
"name": "Quantity_Sum",
"expression": "SUM('Orders.Quantity')"
},
{
"name": "Amount_Sum",
"expression": "SUM('Orders.Amount')"
}
]
}
},
json: true,
};
const report = await request(createOptions);
This will create a pivot table report named "My Pivot Table".
Conclusion
In this blog post, we covered the basics of using Zoho Analytics API to authenticate, retrieve and create reports. You can explore more of the API's endpoints and features in the official documentation. The full examples in this post can be found in this GitHub repository.