Get Your Google Sheets API Key
After you’ve created your own version of Leaflet Maps with Google Sheets or Leaflet Storymaps with Google Sheets, there are two ways to finalize your map, as described above: either save your Google Sheet tabs in CSV format, or get your own Google Sheets API key and paste it into your Leaflet code on GitHub. You’ll learn about the latter method in this section.
Beginning in January 2021, Google Sheets version 4 requires a API (application programming interface) key to allow code to read your data, in order to maintain reasonable limits on use of its services. For Google Sheets, the limit is 500 requests per 100 seconds per project, and 100 requests per 100 seconds per user. There is no daily usage limit.
You can get your own free Google Sheets API key by following the steps below. Overall, you will create and name your Google Cloud project, enable the Google Sheets API to allow a computer to read data from your Google Sheet, copy your new API key, and paste it into the Leaflet code in place of our key.
Before you begin:
- You need a personal Google account, not a Google Suite account issued by your school or business.
- This tutorial presumes that you have already have completed the Leaflet Maps with Google Sheets or Leaflet Storymaps with Google Sheets template above, and wish to finalize your map.
- If you already created a Google Sheets API key for one template above, you can also use that key for another template.
Go to the Google Developers Console at https://console.developers.google.com/ and log in to your Google account. Google may ask you to identify your country and agree to its terms of service.
Click on Create a Project on the opening screen, as shown in Figure 13.26. Or alternatively, go to the upper-left drop-down menu to Select a project > New project.
- In the next screen, give your new project a meaningful short name to remind you of its purpose, such as
handsondataviz. You do not need to create an organization or parent folder. Then click Create, as shown in Figure 13.27.
- In the next screen, press the + Enable APIs and Services at the top of the menu, as shown in Figure 13.28. Make sure that your new project name appears near the top.
- In the next screen, enter Google Sheets into the search bar, and select this result, as shown in Figure 13.29.
- In the next screen, select the Enable button to turn on the Google Sheets API for your project, as shown in Figure 13.30.
- In the left sidebar menu, click Credentials, then click + Create Credentials and select API key, as shown in Figure 13.31.
- In the next screen, the console will generate your API key. Copy it, then press Restrict key, as shown in Figure 13.32.
- In the new window, under API restrictions, choose the Restrict key radio button. In the dropdown that appears, choose Google Sheets API, then click Save, as shown in Figure 13.33.
- In your Leaflet map code on your GitHub repo, open the
google-doc-url.jsfile, click the pencil symbol to edit it, and paste in your Google Sheets API key to replace our key, as shown in Figure 13.34. Be careful not to erase the single-quote marks or the semicolon. Scroll down to Commit your changes.
You might receive a notification from GitHub stating that you have an exposed API key, but don’t worry. This key can only be used with Google Sheets, you received it for free, and you did not attach any billing information to it, so Google cannot charge you for its use.
TODO above: Test our instructions on a new Google account to fix what reviewer Erica H. described here: I did notice when you get to Step 7, they ask you what kind of credentials you’ll need, where you’ll be calling the API from, and the kind of data you will be accessing. See screenshots attached. You might want to add a note about this in your book chapter in case folks don’t know what to select if they are taken to this step automatically. I ended up clicking refresh accidentally and then I saw the + Create Credentials>API key dropdown menu as shown in your chapter instructions and I was able to generate my Google API key without entering those credential specifications
Now that you’ve learned how to create a Google Sheets API key to use with Leaflet Maps with Google Sheets or Leaflet Storymaps with Google Sheets, in the next sections you’ll learn more about other types of Leaflet map templates.