How to fetch employees from Gusto using Python
Gusto, a robust, cloud-based Human Resource Information System (HRIS), is engineered to streamline and automate HR operations. It provides a comprehensive toolkit for payroll, benefits, and HR management, all of which are intricately interconnected to decrease administrative tasks. Moreover, Gusto's API permits custom integration, facilitating businesses to access employee data and optimize their workflow.
To help you get employee data in Gusto via Python, we'll break down every step you need to follow in this article.
Obtaining your access token
To initiate this process, users should be directed to the Gusto authorization URL: <code class='blog_inline-code'>https://api.gusto.com/oauth/authorize</code>. Once the authorization flow is completed, Gusto will redirect the user to the <code class='blog_inline-code'>redirect_url</code> that was initially provided. Included in this redirect will be the authorization code as a query parameter.
The next step in the process is to exchange the authorization code for an access token. This is done by making a POST request to the Gusto token URL: <code class='blog_inline-code'>https://api.gusto.com/oauth/token</code>.
This request should include the client ID and client secret that were provided by Gusto, along with the authorization code and other necessary parameters. These should be passed as body parameters in the POST request.
For instance, a Python request might look something like this:
Upon successful completion of the request, the response will include an <code class="blog_inline-code">access_token</code>, an <code class="blog_inline-code">expires_in</code> value indicating the lifespan of the token, and a <code class="blog_inline-code">refresh_token</code>. The <code class="blog_inline-code">access_token</code> is used in subsequent API calls to pull employee data, while the <code class="blog_inline-code">refresh_token</code> can be used to obtain a new <code class="blog_inline-code">access_token</code> once the original has expired.
Steps for fetching employee data from the Gusto API
First, let's install the necessary package:
Next, we will go through the OAuth 2 flow to get the access token.
Then, we'll request the current user's data:
Next, we'll loop over the companies that the current user is a payroll admin for, and fetch the employees for each company.
Note: This code fetches all employees for each company the current user is a payroll admin for. It uses the <code class="blog_inline-code">include</code> parameter to include all compensations and custom fields for each employee. It also handles pagination by incrementing the page number for each request until an empty page is returned.
An example of an individual item returned by this API Endpoint is:
It’s worth remembering that your clients might be using other HRIS solutions, whether that’s Workday, Namely, UKG, etc.
You can offer customer-facing integrations with all of the HRIS solutions your clients use by connecting to Merge's HRIS Unified API.
Learn more about Merge’s Ticketing Unified API, along with the other features and capabilities provided by the platform, by scheduling a demo with one of our integration experts.