-
Get Google Authentication Token.
This sample uses the Google Service account to authenticate into Google Spreadsheet API. To receive the proper token information, you'll need to set up an account in the Google API Console.
- Set up your project in the Google API console. You can follow the instructions highlighted in Google Account Authentication tutorial here
- Generate a service account and download the access token. You can follow the steps highlighted in Account Authentication - Service Account
- Create an
.env.development
file. Copy into it from the downloaded JSON key file:
GOOGLE_SERVICE_ACCOUNT_EMAIL=copy service account email GOOGLE_PRIVATE_KEY=copy private key
-
Create a Test Spreadsheet.
- Generate a new spreadsheet via Google Sheets
- Grant owner access to the spreadsheet to your service account email address.
- You will need the Sheet ID to properly run the example. Sheet ID can be found in the URL of a Google spreadsheet. For example, you can find it via
https://docs.google.com/spreadsheets/d/<GOOGLE_SHEET_ID>/
. Add this to your.env.development
file as well.
-
Start developing.
To get started, run
npm install
to add all necessary packages.When developing locally, you'll want to include the ENV variables in your
.env.development
. Read more about how Gatsby handles.env
files and environment variables in the Gatsby Docscd examples/functions-google-sheets npm install npm run develop
-
Open the code and start customizing!
Your site is now running at http://localhost:8000! You can use the UI on the index page to test the functions or directly access them at http://localhost:8000/api/sheets
Edit
src/pages/index.js
to see your site update in real-time! -
Deploy
You can deploy this example on Gatsby Cloud by copying the example into a new repo and connecting that to Gatsby Cloud.