https://retellai.com/?utm_source=discord&utm_medium=seo logo
Issue iterating through api call history
# ❓・bug-report
t
I am iterating through my call history via the api to find out how many total Emails I have received for that day. I am having an issue where my code just paginates through literally my entire call history and that takes WAY too long. So I need it to paginate through just that dates worth of calls. Can anyone help me find a fix to this issue?
f
https://docs.retellai.com/api-references/list-calls instead of retrieving ALL the calls and then filter, just filter when retrieving calls.
t
@Florian - support @ Retell AI
Copy code
Filter criteria: {'start_timestamp': 1734588000000, 'end_timestamp': 1734674399999}
Error fetching calls: Error code: 400 - {'error_message': 'request/body/filter_criteria/start_timestamp must be object, request/body/filter_criteria/end_timestamp must be object'}
Exception type: <class 'retell.BadRequestError'>
No calls found with an email in call_analysis.
Copy code
python

def fetch_all_calls_with_filter():
    all_calls = []
    pagination_key = None  # Initialize pagination key

    while True:
        # Correct filter criteria
        filter_criteria = {
            "start_timestamp": today_start,
            "end_timestamp": today_end,
        }

        try:
            # Print filter criteria for debugging
            print(f"Filter criteria: {filter_criteria}")

            # Fetch calls with filter and pagination
            if pagination_key:
                response = client.call.list(
                    filter_criteria=filter_criteria,
                    pagination_key=pagination_key
                )
            else:
                response = client.call.list(
                    filter_criteria=filter_criteria
                )
        except Exception as e:
            print(f"Error fetching calls: {e}")
            print(f"Exception type: {type(e)}")
            break

        # Debug: Print response structure
        print(f"Fetched {len(response)} calls.")

        if not response:
            break  # No calls in the response, stop pagination

        # Append valid calls to the list
        all_calls.extend(response)

        # Set pagination_key to the last call ID if available
        if len(response) > 0:
            pagination_key = response[-1].call_id
        else:
            break  # Exit if no more calls

    return all_calls
having issue with time format
Copy code
python

# Calculate start and end timestamps for the target day
central_offset = timedelta(hours=-6)
central_time_now = datetime.now() + central_offset

today_start = int(datetime.combine(central_time_now - timedelta(days=8), datetime.min.time()).timestamp() * 1000)
today_end = int(datetime.combine(central_time_now - timedelta(days=8), datetime.max.time()).timestamp() * 1000)

start_datetime = datetime.fromtimestamp(today_start / 1000)
end_datetime = datetime.fromtimestamp(today_end / 1000)
print("Start and End Time Parameters")
print(f"Start timestamp: {start_datetime.strftime('%Y-%m-%d %H:%M')}")
print(f"End timestamp: {end_datetime.strftime('%Y-%m-%d %H:%M')}")`
Thats my today start
f
Print out the time format to see what it is, maybe it's the wrong format? it should be an integer. Also look here: https://github.com/RetellAI You can see examples and the library source code. i think you're almost there
t
Hmm it's an integer...
Copy code
<class 'int'>
Filter criteria: {'start_timestamp': 1734847200000, 'end_timestamp': 1734933599999}
Copy code
Filter criteria: {'start_timestamp': 1734847200000, 'end_timestamp': 1734933599999}
Error fetching calls: Error code: 400 - {'error_message': 'request/body/filter_criteria/start_timestamp must be object, request/body/filter_criteria/end_timestamp must be object'}
Exception type: <class 'retell.BadRequestError'>
@Florian - support @ Retell AI
f
https://github.com/RetellAI/retell-python-sdk/blob/main/src/retell/types/call_list_params.py#L38 Try to use lower_threshold: int upper_threshold: int e.g. filter_criteria = { "lower_threshold": 1735624455, "upper_threshold": 1735624455, }