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  Wake Up With a Cup of Java: 10+ Practice Projects For Your Programming Career Prospects

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  Using Vertex AI For Rapid Model Prototyping And Deployment

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  Answering The Top-25 Most Asked Google Cloud Questions Of 2022

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
View Post
  • Computing
  • Multi-Cloud
  • Technology

Wiz: 80% of cloud breaches are caused by basic mistakes

  • April 13, 2026
View Post
  • Computing
  • Multi-Cloud
  • Technology

Contact center monitoring best practices for CX leaders

  • April 9, 2026
View Post
  • Computing
  • Multi-Cloud
  • Technology

Cloud vs. local backup: Which is right for your organization?

  • April 9, 2026
View Post
  • Computing
  • Multi-Cloud
  • Technology

Why channel partners must design for tech sovereignty

  • April 7, 2026
View Post
  • Computing
  • Multi-Cloud
  • Technology

“A lot of other cloud vendors have been let off the hook”: Oracle leans hard on one-size-fits-all appeal of OCI for enterprises

  • March 30, 2026
View Post
  • Computing
  • Technology

Google Cloud and NVIDIA expand AI innovation across industries at GTC 2026

  • March 17, 2026
View Post
  • Computing
  • Multi-Cloud
  • Technology

Last year in AWS with Corey Quinn

  • March 9, 2026
View Post
  • Computing
  • Multi-Cloud
  • Technology

A guide to contact center security best practices

  • March 6, 2026

Stay Connected!
LATEST
  • 1
    Expectations vs. Reality: The AI We Thought We’d Have in 10 Years
    • June 19, 2026
  • digital-nomad-freelancer-worker-2151205464 2
    One paperwork problem – Get your Digital Nomad Visa employment documents fast from UK, EU or Singapore
    • June 16, 2026
  • 3
    Samsung Art Store Brings Art Basel to Homes Worldwide With New Curated Collection
    • June 15, 2026
  • 4
    You Do Not Need to Invest in the IPO of SpaceX, Anthropic, and OpenAI
    • June 10, 2026
  • 5
    The consequences of relying on AI for accurate news
    • June 10, 2026
  • 6
    Connecting AI agents with unstructured data using Google Cloud Storage MCP Servers
    • June 10, 2026
  • 7
    WWDC26: Apple unveils next generation of Apple Intelligence, Siri AI, powerful parental controls, and an expansive set of software improvements
    • June 8, 2026
  • 8
    IBM and Google Cloud Announce Strategic Partnership to Scale AI with Human Expertise and AI‑Powered Delivery
    • June 4, 2026
  • Data center 9
    Data Sovereignty in Spain. It’s Not Just About the Law, It’s About Efficiency
    • June 3, 2026
  • 10
    Ink vs Pixels. What you miss versus what you are actually missing.
    • June 1, 2026
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
    Banks race to patch new cyber vulnerabilities, and other cybersecurity news
    • May 25, 2026
  • pope-leo-xiv-cq5dam-1500.844 2
    Pope Leo XIV to Publish First Encyclical on Artificial Intelligence and Human Dignity on 25 May
    • May 22, 2026
  • 3
    Portfolio to Clients, and is Strengthened by Ongoing Project Glasswing Work
    • May 20, 2026
  • reMarkable Paper Pure 4
    Everything The reMarkable Paper Pure Actually Does
    • May 14, 2026
  • 5
    Scaling cloud and AI: Microsoft Azure’s commitment to Europe’s digital future
    • May 11, 2026
  • /
  • Technology
  • Tools
  • About
  • Contact Us

Input your search keywords and press Enter.