Category search

Get the category information for any business.


1Set up you SafeGraph account

Create or log in to your SafeGraph account and create your API Key.

Now, lets include your API key in the request. Copy your API key and paste it below.

import requests
  
headers = {
    'Content-Type': 'application/json',
    'apikey': 'undefined' 
}

query = ""

req = requests.post(
    'https://api.safegraph.com/v1/graphql',
    json={ 'query': query },
    headers=headers
)

2Setting up your query

The Places API allows you to specify the exact data you want. This means that you can ask for data from Core, Geometry, or Patterns for any location. In this example we are going to be focused around Core Places.

We are going to make a request to search and filter for the Starbucks brands. To narrow the search, we are going to limit it to brands located in Seattle, WA and get the top result.

Select a few of the fields below data you want to include in the response.

SafeGraph Core Fields (more fields here)

SafeGraph Core Fields

Desc

Desc

Desc

Desc

Desc

import requests
  
headers = {
    'Content-Type': 'application/json',
    'apikey': 'undefined' 
}
query = """
query {
    search(filter: {
        brand:"Starbucks"
        address: {
            city: "Seattle"
            region: "WA"
            iso_country_code: "US"
        }
    } first: 1) { 
        safegraph_core {
			naics_code
			top_category
			brands
			category_tags
        } 
    }
}
"""

req = requests.post(
    'https://api.safegraph.com/v1/graphql',
    json={
        'query': query
    },
    headers=headers
)

3Getting a response

After you send the request, you will receive a response that will reflect the fields you included in the query.

{
    "data": {
        "search": [
            {
                "safegraph_core": {
					"naics_code": 722515,
					"top_category": "Restaurants and Other Eating Places",
					"brands": "starbucks reserve", 
					"category_tags": [ 
						"Snacks", 
						"Counter Service", 
						"Dessert", 
						"Tea House", 
						"Coffee Shop", 
						"Bakery"
                    ]
                }
            }
        ]
    },
    "extensions": {
        "row_count": 1,
        "version_date": "1635494405__2021_10"
    }
}


4Parsing the data
Since the response is a JSON object, you can select the data object and parse through the search array.
r_json = req.json()

for i in r_json['data']['search']:
	print(i)
Questions?
Don't see a guide you need?