Geographic Category Search

Find all the stores for a certain category in a location.


1Set up your 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
import json

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

query = ""

req = requests.post(
    'https://api.safegraph.com/v2/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 our Core Places data.

We are going to make a request to search for all the Coffee Shops (NAICS: 722515) location in San Francisco, CA.

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

SafeGraph Core Fields (more fields here)

SafeGraph Core Fields

The name of the place of interest.

Returns a String for the street address of the place of interest.

import requests
import json

headers = {
    'Content-Type': 'application/json',
    'apikey': 'undefined'
}
query = """
query {
  search(filter: {
      address: {
          city: "San Francisco",
          region: "CA",
          iso_country_code: "US"
      }
      naics_code: 722515
  }){
      places {
          results(first: 5, after: "") {
              edges {
                  node {
                      placekey
                      safegraph_core {
							location_name
							street_address
                      }
                  }
              }
          }
      }
  }
}
"""

req = requests.post(
    'https://api.safegraph.com/v2/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": {
      places: {
        results: {
          edges: [
            {
              node: {
                "placekey": "222-222@5vg-7f8-49z",
                "safegraph_core": {
					"location_name": "Cafe DeStijl",
					"street_address": "1 Union St",
                }
              }
            },
            {
              node: {
                "placekey": "222-222@5vg-7f9-qpv",
                "safegraph_core": {
					"location_name": "Cafe Franco",
					"street_address": "240 Fort Mason Bldg 240",
                }
              }
            },
            {
              node: {
                "placekey": "222-222@5vg-7gj-wkz",
                "safegraph_core": {
					"location_name": "BBC Cafe",
					"street_address": "1485 Bay Shore Blvd",
                }
              }
            },
            {
              node: {
                "placekey": "222-222@5vg-7gq-6hq",
                "safegraph_core": {
					"location_name": "Nina's Cafe",
					"street_address": "201 Fell St",
                }
              }
            },
            {
              node: {
                "placekey": "222-222@5vg-7gq-7qz",
                "safegraph_core": {
					"location_name": "Grand Parade Coffee",
					"street_address": "1390 Market St Ste 200",
                }
              }
            },
            {
              node: {
                "placekey": "222-222@5vg-7gq-q9f",
                "safegraph_core": {
					"location_name": "George and Lennie",
					"street_address": "277 Golden Gate Ave",
                }
              }
            },
            {
              node: {
                "placekey": "222-222@5vg-7gq-t35",
                "safegraph_core": {
					"location_name": "Chez Nous Cafe",
					"street_address": "1145 Market St",
                }
              }
            },
            {
              node: {
                "placekey": "222-222@5vg-7gq-q9f",
                "safegraph_core": {
					"location_name": "127 Cafe",
					"street_address": "1275 Market St",
                }
              }
            },
            {
              node: {
                "placekey": "222-222@5vg-7gq-q9f",
                "safegraph_core": {
					"location_name": "Pete's Deli & Cafe",
					"street_address": "1661 Divisadero St",
                }
              }
            },
            {
              node: {
                "placekey": "222-222@5vg-7gq-q9f",
                "safegraph_core": {
					"location_name": "Starbucks",
					"street_address": "1750 Divisadero St",
                }
              }
            }
          ]
        }
      }
    }
  },
  "extensions": {
      "row_count": 10,
      "version_date": "1635494405__2021_10"
  }
}


4Parsing the data
Since the response is a JSON object, you can select the data lookup object and pull the fields you need.
r_json = req.json()
edges = r_json['data']['search']['places']['results']['edges']

print(json.dumps(edges, indent=4, sort_keys=True))