How to Integrate Google APIs in Your Next.js Project

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MyrinNew
    Senior Member
    • Feb 2024
    • 5175

    #1

    How to Integrate Google APIs in Your Next.js Project

    Ever thought about using Google Docs or Sheets as more than just documents or spreadsheets? Imagine them as dynamic content sources or even simple databases for your applications. In this guide, we'll dive into how to set up Google's APIs to unlock these powerful services within your projects, transforming how you manage and access data.


    The specific version of Next.js you're using won't impact this guide, as our focus is solely on obtaining API keys from Google's platform.


    We will first need to create a new Service Account to do so go here


    Click on Start Free





    Read the terms of service and click Agree & Continue.

    1. Create your Payment Profile (you won't be charged).
    2. Select Individual Profile Type if you're not a business.
    3. Click Create.
    4. Now add a payment method.
    5. Click Start Free.
    6. Select your Cloud Platform options.


    Great! Now that we have a Service Account set up, look for the menu on the left and click on APIs & Services.





    Enable APIs & Services





    Search for Google Sheets API and select it





    Click Enable


    Great now we have the google sheets API enabled now let's create the credentials so we can programmatically connect to it.


    Click on Create Credentials





    Select Application Data & click Done





    Now click on the IAM & Admin in the left menu and then click Service Accounts





    Now click on Create Service Account





    Give your account a name and a description and click Create And Continue


    Select Owner as the Role and click Continue


    We can click Done on the final step.


    Now we can manage the keys for this account which will allow us to connect to Google Sheets.


    Click on the 3 dots and select Manage Keys





    Click on Add Key and then on Create new key





    Select the JSON option and click Create





    Save it somewhere safe and then open it in your code editor


    Great all we really care about are the private_key and the client_email, these will allow us to establish the connection.


    Go back to your Nextjs Project and in the .env.local (if you don't have a .env file yet create one in the root of your project) file let's create the environment variables we will need.






    # .env.local

    GOOGLE_SERVICE_ACCOUNT_EMAIL=
    GOOGLE_PRIVATE_KEY=







    Paste your keys from the JSON file in here.


    Awesome! Now you are ready to start integrating google sheets like I did here to store Newsletter subscribers from another nextjs app!


    Happy coding! 😊




    More...
Working...