Leveraging Python to Access Data from the Google Places API

Daniel Blanc
3 min readSep 4, 2023

--

In this tutorial, we’ll explore how to retrieve data from Google Places API using Python. This is a great skill to have, as APIs (Application Programming Interfaces) often provide a wealth of information that can be programmatically accessed and used in your projects.

If you want to know more about APIs, check this article:

In our example, we’ll be using the following Google Places API URL template:

https://maps.googleapis.com/maps/api/place/findplacefromtext/json?input=<address_input>&inputtype=textquery&fields=formatted_address%2Cname%2Cbusiness_status%2Cplace_id&key=<api_key>

This URL searches for places based on the text input (an address, in this case) and retrieves the formatted address, place name, business status, and place id.

To follow this tutorial, you will need a basic understanding of Python and have Python 3 installed on your computer.

Step 1: Get Your Google API Key

Before you start, you’ll need a Google API Key. You can get this key by following the instructions on the Google Cloud Documentation.

Please note that using Google’s API may incur costs, so be sure to understand their pricing before you start.

Step 2: Installing Required Libraries

For this tutorial, we’ll be using the requests library in Python to make HTTP requests to the API. If you haven’t installed it yet, you can do so by running the following command in your terminal:

pip install requests

Step 3: Making Requests to the Google Places API

Now let’s write some Python code to access the Google Places API. The code will take an address as input, substitute the address and the API key into the URL, and then use the `requests` library to make an HTTP GET request to the API.

import requests
def get_place_info(address, api_key):
# Base URL
base_url = "https://maps.googleapis.com/maps/api/place/findplacefromtext/json"
# Parameters in a dictionary
params = {
"input": address,
"inputtype": "textquery",
"fields": "formatted_address,name,business_status,place_id",
"key": api_key,
}
# Send request and capture response
response = requests.get(base_url, params=params)
# Check if the request was successful
if response.status_code == 200:
return response.json()
else:
return None

Step 4: Testing Our Function

Now let’s test our function. Replace `YOUR_API_KEY` with your actual Google API Key and `ADDRESS` with the address you’re searching for:

api_key = "YOUR_API_KEY"
address = "ADDRESS"
place_info = get_place_info(address, api_key)
if place_info is not None:
print(place_info)
else:
print("Failed to get a response from Google Places API")

After running the above script, you should see a JSON response printed on your console. This response contains the data (formatted address, place name, business status, and place id) of the place associated with the address you entered.

Conclusion

Congratulations! You’ve learned how to access the Google Places API using Python. This tutorial is just the tip of the iceberg when it comes to what’s possible with Google’s APIs and Python. Feel free to explore the official Google Places API documentation to understand more about different query parameters and more API functionalities. Happy coding!

--

--