aster.cloud aster.cloud
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
aster.cloud aster.cloud
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
  • Computing
  • Engineering
  • Software Engineering

Calling Google APIs From Serverless (Part I: Cloud APIs)

  • aster.cloud
  • April 18, 2022
  • 4 minute read

Introduction

As Google Cloud users, you may already be familiar with Google App Engine, Cloud Functions, or Cloud Run, our serverless platforms allowing technical practitioners to focus on building solutions, not what they run on. Google as a whole, has many divisions which provide developer tools beyond these platforms, so a common question which arises is: How do you call Google APIs from serverless?

We’ll tackle this query in two phases, starting with how to access Cloud APIs from serverless. Most Cloud APIs have their own product client libraries you’ll use from serverless (and even non-serverless) platforms. Below, we’ll demonstrate a sample app calling Cloud APIs from serverless to kick off this conversation. Non-Cloud Google APIs operate differently. You’ll likely have to use a platform client library as individual products may not have their own client libraries. One key difference that sets Cloud and non-Cloud APIs apart is that their security models differ depending on who owns the data your apps try to access, and this is where we’ll dive in.


Partner with aster.cloud
for your next big idea.
Let us know here.



From our partners:

CITI.IO :: Business. Institutions. Society. Global Political Economy.
CYBERPOGO.COM :: For the Arts, Sciences, and Technology.
DADAHACKS.COM :: Parenting For The Rest Of Us.
ZEDISTA.COM :: Entertainment. Sports. Culture. Escape.
TAKUMAKU.COM :: For The Hearth And Home.
ASTER.CLOUD :: From The Cloud And Beyond.
LIWAIWAI.COM :: Intelligence, Inside and Outside.
GLOBALCLOUDPLATFORMS.COM :: For The World's Computing Needs.
FIREGULAMAN.COM :: For The Fire In The Belly Of The Coder.
ASTERCASTER.COM :: Supra Astra. Beyond The Stars.
BARTDAY.COM :: Prosperity For Everyone.

API security

Most Cloud APIs have their own product client libraries and use a combination of service accounts (for authentication) and Cloud IAM permissions (for authorization). To ease the onboarding process, all Cloud compute platforms (including serverless) provide default service accounts which are granted a wide variety of permissions. Once you’ve finished prototyping and moving to production, follow the best practice of “least privilege” with user-managed service accounts providing only the permissions needed for your app to function properly. The sample app doesn’t show these steps for simplicity, but they should be employed for your apps.

Read More  4 Essential Characteristics Of Successful APIs

To simplify the onboarding process, we’ll use the default service accounts for the sample app. When you eventually switch to user-managed service accounts, your code doesn’t need to change as the Application Default Credentials (ADC) library looks for the most appropriate service account to use. With that in mind, let’s take a look at the sample app.

The application

The app demonstrates use of the Cloud Translation API, one of our AI/ML “building block” APIs (now part of our Vertex AI offering). If Google Translate had an API, Cloud Translation would be it. This API is used to implement a minimal “Google Translate” from English to Spanish. The app prompts for the text to translate, calls Cloud Translation to do the heavy lifting, then displays the results in the browser. Python and Node.js versions of the app are available in its open source repo. At the heart of the application is its translate() function, shown in pseudocode below:

The heart of the application, the translate() function

 

Use of Cloud APIs requires several lines of code:

  • At the top is the import/require that connects your app to the Cloud Translation client library.
  • The very next line instantiates an API client with which to connect to the API. Here the service account credentials (ADC) are checked, whether the default service account demonstrated here, or the user-managed service account you’ll use for your app.
  • Once an API client has been created, you can access any API functionality the service account has permissions for, in this case, we have one call to the API near the middle to perform the actual translation.
Read More  Announcing Data-Driven Styling: Style Google Map Boundaries To Convey Key Information

That’s it. Both apps feature less than 50 lines of actual code. All other lines are used only for application purposes. When we cover non-Cloud Google APIs in the next post, you’ll observe code that looks similar. The client libraries and auth types will change, but the “flow” will roughly be the same: 1) import the library, 2) create an API client, 3) use the client object to access API functionality.

Choose a serverless platform to deploy the sample app to. Once deployed to the cloud, accessing it to make one translation of “hello world” results in program output like what you see below. The output will look identical for all three platforms, but the screenshot featured is the Cloud Run version:

“My Google Translate” MVP app (Cloud Run edition)

 

 

 

While this app demonstrates accessing Cloud APIs from serverless, it has been written with another purpose: it can be deployed to App Engine, Cloud Functions, or Cloud Run, with no code changes. (The differences are managed in the configuration.) For users new to our serverless platforms, this will help you learn them better, gain a greater understanding of how they’re similar and different from each other, and lastly how to write code that can run on any of them in case you pick one but then decide later another one is a better fit. This means you, “can’t make a mistake and pick the wrong platform,” when you can “shift” between them. Also see this companion post covering the same sample app but from the perspective of the configuration changes necessary to get it to run on another Cloud serverless platform.

Read More  Study By Forrester: The Industry’s Take On 5G Networks On Cloud

Next steps

In addition to the code, the repo also links you to various codelabs (self-paced, hands-on tutorials) that guide you in building and deploying this app to any of the serverless platforms (or running locally) in either Python or Node.js. An active Google Cloud billing account is required to deploy to the cloud, but you shouldn’t incur charges if you run it briefly and stay within each product’s “Always Free” tier. More information can be found in the “Cost” section in the README. Once you know how to call one Cloud API, accessing others will be quite similar. See you next time when we’ll cover how to access non-Cloud Google APIs from serverless!

 

 

By: Wesley Chun (Developer Advocate)
Source: Google Cloud Blog


For enquiries, product placements, sponsorships, and collaborations, connect with us at [email protected]. We'd love to hear from you!

Our humans need coffee too! Your support is highly appreciated, thank you!

aster.cloud

Related Topics
  • API
  • Google Cloud
  • Node js
  • Python
  • Serverless
You May Also Like
Getting things done makes her feel amazing
View Post
  • Computing
  • Data
  • Featured
  • Learning
  • Tech
  • Technology

Nurturing Minds in the Digital Revolution

  • April 25, 2025
View Post
  • Computing
  • Public Cloud
  • Technology

United States Army Enterprise Cloud Management Agency Expands its Oracle Defense Cloud Services

  • April 15, 2025
View Post
  • Engineering
  • Technology

Guide: Our top four AI Hypercomputer use cases, reference architectures and tutorials

  • March 9, 2025
View Post
  • Software Engineering
  • Technology

Claude 3.7 Sonnet and Claude Code

  • February 25, 2025
Microsoft’s Majorana 1 chip carves new path for quantum computing
View Post
  • Computing
  • Technology

Microsoft’s Majorana 1 chip carves new path for quantum computing

  • February 19, 2025
View Post
  • Computing
  • Engineering

Why a decades old architecture decision is impeding the power of AI computing

  • February 19, 2025
CES 2025: Intel Shows Off Its AI Tech
View Post
  • Computing
  • Technology

CES 2025: Intel Shows Off Its AI Tech

  • January 23, 2025
View Post
  • Engineering
  • Software Engineering

This Month in Julia World

  • January 17, 2025

Stay Connected!
LATEST
  • college-of-cardinals-2025 1
    The Definitive Who’s Who of the 2025 Papal Conclave
    • May 7, 2025
  • conclave-poster-black-smoke 2
    The World Is Revalidating Itself
    • May 6, 2025
  • oracle-ibm 3
    IBM and Oracle Expand Partnership to Advance Agentic AI and Hybrid Cloud
    • May 6, 2025
  • 4
    Conclave: How A New Pope Is Chosen
    • April 25, 2025
  • Getting things done makes her feel amazing 5
    Nurturing Minds in the Digital Revolution
    • April 25, 2025
  • 6
    AI is automating our jobs – but values need to change if we are to be liberated by it
    • April 17, 2025
  • 7
    Canonical Releases Ubuntu 25.04 Plucky Puffin
    • April 17, 2025
  • 8
    United States Army Enterprise Cloud Management Agency Expands its Oracle Defense Cloud Services
    • April 15, 2025
  • 9
    Tokyo Electron and IBM Renew Collaboration for Advanced Semiconductor Technology
    • April 2, 2025
  • 10
    IBM Accelerates Momentum in the as a Service Space with Growing Portfolio of Tools Simplifying Infrastructure Management
    • March 27, 2025
about
Hello World!

We are aster.cloud. We’re created by programmers for programmers.

Our site aims to provide guides, programming tips, reviews, and interesting materials for tech people and those who want to learn in general.

We would like to hear from you.

If you have any feedback, enquiries, or sponsorship request, kindly reach out to us at:

[email protected]
Most Popular
  • 1
    Tariffs, Trump, and Other Things That Start With T – They’re Not The Problem, It’s How We Use Them
    • March 25, 2025
  • 2
    IBM contributes key open-source projects to Linux Foundation to advance AI community participation
    • March 22, 2025
  • 3
    Co-op mode: New partners driving the future of gaming with AI
    • March 22, 2025
  • 4
    Mitsubishi Motors Canada Launches AI-Powered “Intelligent Companion” to Transform the 2025 Outlander Buying Experience
    • March 10, 2025
  • PiPiPi 5
    The Unexpected Pi-Fect Deals This March 14
    • March 13, 2025
  • /
  • Technology
  • Tools
  • About
  • Contact Us

Input your search keywords and press Enter.