How to access the Places API via R?

Hello! I am new to programming and am learning to access the API. I have a lot of experience using R, but not much with other programming languages. Are there others in this same boat? I am thinking the best use of my time will be to learn enough Python to get by?


This topic was automatically generated from Slack. You can find the original thread here.

Hey @Angela_Rout_UBC ! Wondering if it might be helpful to meet with Pranav, our Community Data Scientist, for some help on this. Let me see if that’s something we can arrange. I’ll circle back with you on this!

@Angela_Rout_UBC With some trial and error, I was able to use the following blogpost to access the API via R:

I passed a named list with the apikey, i.e., list(apikey="key-goes-here")as the header argument in the conn <- GraphqlClient$new() call.

I started with a simple query, and built up using the safegraph API tool. It took a bit, but worked just fine (aside from rate-limits).

Any R user have an idea why this doesn’t work? I know my API key is valid. It gives me HTTP 400. BTW I pulled this directly from intermediate steps in the SafegraephR package.


query='query($placekeys: [Placekey!]) {
  batch_lookup(placekeys: $placekeys) {
    placekey
    safegraph_weekly_patterns (date: "2021-01-07") {
      parent_placekey
      location_name
      street_address
      city
      region
      postal_code
      iso_country_code
      date_range_start
      date_range_end
      raw_visit_counts
      raw_visitor_counts
      visits_by_day
      visits_by_each_hour
      poi_cbg
      visitor_home_cbgs
      visitor_home_aggregation
      visitor_daytime_cbgs
      visitor_country_of_origin
      distance_from_home
      median_dwell
      bucketed_dwell_times
      related_same_day_brand
      related_same_week_brand
      brands {
        brand_id
        brand_name
      }
    }
  }
}'

conn <- ghql::GraphqlClient$new(url = "https://api.safegraph.com/v2/graphql", 
                                headers = list(apikey = key))

variable=list(placekeys=c('sg:001341fe7e794ab6bd65bb80759a1ac'))
new <- ghql::Query$new()$query("link", query)

result <- conn$exec(new$link, variables = variable)

Looping in @vchen who can help with this!

2 things here.

  1. that is not a valid Placekey (that looks like a SafeGraph ID). Placekeys look like this: zzy-222@5vg-7mw-dy9
  2. in v2 the calls for visits_by_day and visits_by_hour are now objects to contain time_interval so you can specify the start and end times for the visits count. (before we just returned an array)

updated query call:

query='query($placekeys: [Placekey!]) {
  batch_lookup(placekeys: $placekeys) {
    placekey
    safegraph_weekly_patterns (date: "2021-01-07") {
      parent_placekey
      location_name
      street_address
      city
      region
      postal_code
      iso_country_code
      date_range_start
      date_range_end
      raw_visit_counts
      raw_visitor_counts
      visits_by_day { visits }
      visits_by_each_hour { visits }
      poi_cbg
      visitor_home_cbgs
      visitor_home_aggregation
      visitor_daytime_cbgs
      visitor_country_of_origin
      distance_from_home
      median_dwell
      bucketed_dwell_times
      related_same_day_brand
      related_same_week_brand
      brands {
        brand_id
        brand_name
      }
    }
  }
}'

Sample variable input:

variable = list( placekeys = c("zzy-222@5vg-7mw-dy9", "23y-222@5vg-7gt-fvf", "22z-222@5vg-7gv-4vz"))