Set up Environment Variables:
Create a .env.local file in the root of the project and add the following variables:GOOGLE_SERVICE_ACCOUNT_EMAIL=your_service_account_email@developer.gserviceaccount.com
GOOGLE_PRIVATE_KEY=-----BEGIN PRIVATE KEY-----\nYourPrivateKeyHere\n-----END PRIVATE KEY-----\n
GOOGLE_SHEET_ID=your_google_sheet_id
GOOGLE_SERVICE_ACCOUNT_EMAIL: The email address of your Google Cloud service account.GOOGLE_PRIVATE_KEY: The private key associated with your service account. Make sure to format it correctly, replacing literal newlines in the key file with \n characters within the string.GOOGLE_SHEET_ID: The ID of the Google Sheet where you want to store the emails. You can find this in the URL of your Google Sheet (https://docs.google.com/spreadsheets/d/YOUR_SHEET_ID/edit).
How to get Google Credentials (Detailed Steps):
Getting the necessary Google credentials involves interacting with the Google Cloud Platform. Follow these steps carefully:- Go to Google Cloud Console: Open your web browser and navigate to the Google Cloud Console. You might need to sign in with your Google account.
- Create or Select a Project:
- At the top of the page, click the project dropdown menu (it might say "Select a project").
- If you have an existing project you want to use, select it.
- If not, click "NEW PROJECT". Give your project a name (e.g., "Waitlist App") and click "CREATE".
- Enable the Google Sheets API:
- In the search bar at the top, type "Google Sheets API" and select it from the results.
- Click the "ENABLE" button. If it's already enabled, you can skip this step. This allows your application to interact with Google Sheets.
- Create a Service Account:
- In the search bar, type "Service Accounts" and select it.
- Click "+ CREATE SERVICE ACCOUNT" near the top.
- Enter a "Service account name" (e.g., "waitlist-sheet-updater"). The "Service account ID" will be generated automatically. You can add an optional description.
- Click "CREATE AND CONTINUE".
- For "Grant this service account access to project", you can skip adding roles for now by clicking "CONTINUE".
- For "Grant users access to this service account", you can also skip this and click "DONE".
- Generate a Private Key:
- You should now see your newly created service account in the list. Click on its email address.
- Go to the "KEYS" tab.
- Click "ADD KEY" and choose "Create new key".
- Select "JSON" as the key type and click "CREATE".
- A JSON file containing your credentials will be downloaded automatically. Keep this file secure and private!
- Extract Credentials for
.env.local:- Open the downloaded JSON file with a text editor.
- Find the
"client_email" value. Copy this entire email address (e.g., waitlist-sheet-updater@your-project-id.iam.gserviceaccount.com) and paste it as the value for GOOGLE_SERVICE_ACCOUNT_EMAIL in your .env.local file. - Find the
"private_key" value. Copy the entire string starting from -----BEGIN PRIVATE KEY----- all the way to -----END PRIVATE KEY-----\n. - Important: When pasting this key into your
.env.local file for the GOOGLE_PRIVATE_KEY variable, you MUST replace all the literal newline characters (\n within the key string itself in the JSON file) with the two characters \ and n. It should look like one long line in your .env.local file, containing \n where the line breaks were.
Example: -----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBg...YourKey...\n...MoreKey...\n-----END PRIVATE KEY-----\n becomes GOOGLE_PRIVATE_KEY=-----BEGIN PRIVATE KEY-----\nYourKey...\n...MoreKey...\n-----END PRIVATE KEY-----\n
- Get Your Google Sheet ID:
- Open the Google Sheet you want to use for storing emails.
- Look at the URL in your browser's address bar. It will look something like
https://docs.google.com/spreadsheets/d/1aBcDeFgHiJkLmNoPqRsTuVwXyZaBcDeFgHiJkLmNoPq/edit#gid=0. - The long string of characters between
/d/ and /edit is your Sheet ID (in the example: 1aBcDeFgHiJkLmNoPqRsTuVwXyZaBcDeFgHiJkLmNoPq). - Copy this ID and paste it as the value for
GOOGLE_SHEET_ID in your .env.local file.
- Share Your Google Sheet:
- In your Google Sheet, click the "Share" button (usually top right).
- In the "Add people and groups" field, paste the service account email address you copied in step 6 (e.g.,
waitlist-sheet-updater@your-project-id.iam.gserviceaccount.com). - Make sure it has "Editor" permissions.
- Click "Send" or "Share". This allows the service account (and thus your application) to write data to the sheet.
Now your .env.local file should be correctly configured with the necessary credentials.