Making API Calls with Lists in Python Requests

Feb 3, 2024 ยท 2 min read

The Python Requests library provides a simple way to make HTTP requests in Python. A common use case is making API calls, where you may need to pass a list of items to the API.

Here's an example of making an API call with a list of IDs using Requests:

import requests

ids = [123, 456, 789]

url = 'https://api.example.com/get_users'

response = requests.get(url, params={'ids': ids})

print(response.json())

We create a list of IDs, then pass that list to the params argument of the .get() method. This converts the list to a query parameter that looks like:

ids=123,456,789

The API server can then handle that list of IDs on their end.

Handling Large Lists

One consideration with lists is that many APIs have limits on the URL length. So if you have a very large list, you may run into errors.

Instead of passing the raw list, it's better to join the IDs with commas into a string like "123,456,789". Then the API can split it back into an array.

ids = [123, 456, 789, ...thousands more...]
id_str = ",".join([str(id) for id in ids]) 

response = requests.get(url, params={'ids': id_str})

This keeps the URL from getting too long.

In Summary

  • The Python Requests library provides an easy way to call APIs
  • You can pass lists of data, like IDs, to be handled by the API
  • For large lists, join items into a comma separate string to avoid errors
  • Browse by tags:

    Browse by language:

    The easiest way to do Web Scraping

    Get HTML from any page with a simple API call. We handle proxy rotation, browser identities, automatic retries, CAPTCHAs, JavaScript rendering, etc automatically for you


    Try ProxiesAPI for free

    curl "http://api.proxiesapi.com/?key=API_KEY&url=https://example.com"

    <!doctype html>
    <html>
    <head>
        <title>Example Domain</title>
        <meta charset="utf-8" />
        <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
    ...

    X

    Don't leave just yet!

    Enter your email below to claim your free API key: