Python code for API Call

44 views
Skip to first unread message

Yash Kumar

unread,
Jun 22, 2023, 1:36:30 PM6/22/23
to Zengine Development
Hi Team, 

I am trying to pull data from different tabs within the portal. Tried using sample access token provided in the https://platform.zenginehq.com/account/developer. Here is a sample code in Python:

from urllib.request import urlopen  
import json
import requests


url = "https://api.zenginehq.com/v1/forms/41766/fields.json?access_token=xxxx"

# store the URL in url as
# parameter for urlopen
 
# store the response of URL
response = requests.get(url).json()

data_res = response['data']


However, this code doesn't fetch me data present in individual tabs. Can you guide me on how to create a url and access token that fetch all the records within each tab of the portal? Any guidance would be greatly appreciated

Warm Regards,
Yash Kumar

Wes

unread,
Jun 23, 2023, 9:47:25 AM6/23/23
to Zengine Development
Hello,
The /fields resource will return the form field schema. It sounds like you want the form record data. The records resource is https://api.zenginehq.com/v1/forms/ID/records

This will return the records for one form ID. If you want to return records that correspond to other tabs in the UX, you would need to make additional requests using those form ids.

Yash Kumar

unread,
Jun 29, 2023, 1:21:08 PM6/29/23
to Zengine Development
Thanks for answering, it was really helpful. Now I am able to see records of each form that I want to access. But I have few follow up questions -

  1. Only 20 records are shown for each of the form, is there a way to fetch all the records at one go?
  2. How to fetch the column names of each record as displayed in the portal, the columns names that I am seeing are like "fields123456" 
  3. Currently I am using the access token as per this source https://platform.zenginehq.com/account/developer which expires after 1 hour, is there a way to create a access token that doesn't expire since we are trying to create a pipeline to use the data in our environment?

Here is code below for reference - 

from urllib.request import urlopen  
import json
import requests


  
# store the response of URL
response = requests.get(url).json()
data_res = response['data']


Looking forward for your reply.

Thanks,
Yash Kumar

Wes

unread,
Jun 30, 2023, 9:53:01 AM6/30/23
to Zengine Development
1. You can request more than 20 records if you pass a limit and you can paginate the results if you pass a page number. See https://zenginehq.github.io/developers/rest-api/conventions/querying-options/#pagination

2. You can fetch the field definitions using your original request - https://api.zenginehq.com/v1/forms/id/fields

3. You can request a permanent access token here - https://webportalapp.com/webform/developer-application-registration

Yash Kumar

unread,
Mar 29, 2024, 5:03:14 PM3/29/24
to Zengine Development
Thanks for the above solution. 

I am able to get a permanent access token as well as I can extract more than 20 records at once. However, i am encountering issue in paginating the form and extracting the results. PFB my issues -

How to paginate the form, below is my current code which is capable of extracting max 5000 records at once but have 7565 records in that form ?
# url link

# extract total records present
response = requests.get(eg_url)
eg_response = response.json()
total_count = eg_response['totalCount']

# set limit parameters while fetching data from url
params = {'limit': total_count, 'offset': 0}
response = requests.get(eg_url, params=params)
eg_response = response.json()
eg_data = pd.DataFrame(eg_response['data'])


Looking forward hearing from you.

Wes

unread,
Apr 1, 2024, 10:17:16 AM4/1/24
to Zengine Development
The pagination options from the link above are limit and page, not offset. You will need to loop and make multiple requests, incrementing the page number each time to fetch records beyond page 1.
Reply all
Reply to author
Forward
0 new messages