Getting billing effective date from Deals using API

I am trying to extract data from hubspot deals using the access token given by my mentor. Blow is my code for the extraction,

import requests
import json
import urllib
import csv
# Set your variables
max_results = 3100
access_token = 'my access token'
limit = 100
deal_list = []
get_all_deals_url = "https://api.hubapi.com/crm/v3/objects/deals"

# Set up the headers, including the OAuth 2.0 access token
headers = {
'Authorization': f'Bearer {access_token}',
'Content-Type': 'application/json'
}

# Set up the initial parameters
parameter_dict = {
'limit': limit,
}

while True:
# Make the API request
response = requests.get(get_all_deals_url, headers=headers, params=parameter_dict)

# Check if the request was successful
if response.status_code == 200:
# Parse the JSON response
data = response.json()

# Print the first deal data to inspect the structure
if data['results']:
print(json.dumps(data['results'][0], indent=4))
deal_list.extend(data['results'])

# Check if there are more pages
if data.get('paging', {}).get('next', {}).get('after'):
parameter_dict['after'] = data['paging']['next']['after']
else:
break
else:
# If the request was not successful, print the error
print(f"Failed to retrieve deals: {response.status_code}")
print(response.text)
break

# Extract the necessary properties and write to a CSV
with open('deals.csv', 'w', newline='', encoding='utf-8') as output_file:
writer = csv.writer(output_file)
writer.writerow(['id', 'created_at', 'updated_at', 'deal_name', 'is_archived']) # Header

for deal in deal_list:
deal_id = deal.get('id')
created_at = deal.get('createdAt')
updated_at = deal.get('updatedAt')
deal_name = deal.get('properties', {}).get('dealname')
is_archived = deal.get('archived')

writer.writerow([deal_id, created_at, updated_at, deal_name, is_archived])

here is the output that I have,

{
    “id”: “…”,
    “properties”: {
        “amount”: “4342”,
        “closedate”: “2019-10-22T06:00:00Z”,
        “createdate”: “2018-10-04T07:00:00Z”,
        “dealname”: “A Good Timeout - New Deal”,
        “dealstage”: “closedlost”,
        “hs_lastmodifieddate”: “2019-10-09T06:15:20.930Z”,
        “hs_object_id”: “…”,
        “pipeline”: “default”
    },
    “createdAt”: “2020-10-05T07:00:00Z”,
    “updatedAt”: “2023-09-06T06:15:20.930Z”,
    “archived”: false
}

the issue that I have is that it does not return the source of a deal property named ‘billing effective date’.

Hi @hmiao :waving_hand:

The endpoint you are calling allows you to specify properties to be returned using the “properties” querystring parameter. In this case, an individual property is specified using its internal “name”. Assuming your target property, ‘Billing Effective Date’, has the internal name “billing_effective_date”, you can add this to your request parameters like this:

...
# Set up the initial parameters
parameter_dict = {
'limit': limit,
'properties': 'billing_effective_date'
}
...

Note, to return multiple properties, separate them with a comma (e.g. ‘billing_effective_date,example_property_x’)

Finding the internal “name” of a property can be done by:

  1. HubSpot web UI
    • Settings > Properties > {Object Type} > {Property} Edit > Click the “</>” icon to the right of the property “Label”
  2. HubSpot Properties API

I hope this proves useful. Please let me know if you have any follow-up questions.

Thank you so much for response, I tried and my output looks like this,

{ “id”: “…”, “properties”: { “createdate”: “2020-10-05T07:00:00Z”, “hs_lastmodifieddate”: “2023-09-06T06:15:20.930Z”, “hs_object_id”: “…” }, “createdAt”: “2020-10-05T07:00:00Z”, “updatedAt”: “2023-09-06T06:15:20.930Z”, “archived”: false }

Hi @hmiao :waving_hand:

Looks like your target property was not returned. I recommend double-checking the following:

  1. The ‘billing effective date’ property is in fact a Deal property within the HubSpot portal you are querying.
  2. You are referencing the property’s internal name exactly in your request. This can be checked by following the instructions from my previous message.

Let me know how you go :slightly_smiling_face:

@hmiao - I also found out this:

  • If the Billing Start Date is a specific date, the

    hs_recurring_billing_start_date will be a specific date, otherwise it will return “null”

  • If the Billing Start Date is a delay of days or months, then check the values for hs_billing_start_delay_days or hs_billing_start_delay_months - these are automatically set by HubSpot