
Before you begin
- Labs create a Google Cloud project and resources for a fixed time
- Labs have a time limit and no pause feature. If you end the lab, you'll have to restart from the beginning.
- On the top left of your screen, click Start lab to begin
Create the API proxy
/ 20
Create the full access API product
/ 20
Create the read-only API product, app developer, and 2 apps
/ 20
Add the quota policy
/ 10
Add CORS to the API proxy
/ 20
Create the integrated developer portal
/ 10
APIs are designed to be consumed by app developers, who leverage APIs to provide unique experiences for their users. Google Cloud's Apigee API Platform can be used to publish APIs and make them available for app developers to consume.
In this lab, you create an Apigee API proxy, which requires API key verification to restrict access to the API.
You create API products to provide different levels of service for internal and external application developers. You use a Quota policy to limit the number of calls for each specific application. You create a CORS policy to add Cross-Origin Resource Sharing functionality to the API, which allows it to be called from web applications. You then create a developer portal and publish the API products for app developers to consume.
In this lab, you learn how to perform the following tasks:
Read these instructions. Labs are timed and you cannot pause them. The timer, which starts when you click Start Lab, shows how long Google Cloud resources are made available to you.
This hands-on lab lets you do the lab activities in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials you use to sign in and access Google Cloud for the duration of the lab.
To complete this lab, you need:
Click the Start Lab button. If you need to pay for the lab, a dialog opens for you to select your payment method. On the left is the Lab Details pane with the following:
Click Open Google Cloud console (or right-click and select Open Link in Incognito Window if you are running the Chrome browser).
The lab spins up resources, and then opens another tab that shows the Sign in page.
Tip: Arrange the tabs in separate windows, side-by-side.
If necessary, copy the Username below and paste it into the Sign in dialog.
You can also find the Username in the Lab Details pane.
Click Next.
Copy the Password below and paste it into the Welcome dialog.
You can also find the Password in the Lab Details pane.
Click Next.
Click through the subsequent pages:
After a few moments, the Google Cloud console opens in this tab.
Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud. Cloud Shell provides command-line access to your Google Cloud resources.
Click Activate Cloud Shell at the top of the Google Cloud console.
Click through the following windows:
When you are connected, you are already authenticated, and the project is set to your Project_ID,
gcloud
is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.
Output:
Output:
gcloud
, in Google Cloud, refer to the gcloud CLI overview guide.
To open the Apigee console:
Apigee
, and then click Apigee API Management in the search results.The Apigee console opens, and the landing page shows quick links to commonly used locations.
Apigee is now pinned to the Navigation menu.
In this task, you create an Apigee API proxy that acts as a facade for a backend service. The API proxy will use a service account to allow it to present OpenID Connect identity tokens to the Cloud Run service.
A backend service named simplebank-rest
has already been created and deployed to Cloud Run.
Save this URL. It will be used when creating the API proxy.
In the Apigee navigation menu, select Proxy development > API proxies.
To create a new proxy using the proxy wizard, click +Create.
You will create a reverse proxy for your backend service.
For Proxy template, select General template > Reverse proxy (Most common).
Specify the following for the Proxy details:
Property | Value |
---|---|
Proxy name | bank-v1 |
Base path | /bank/v1 |
Target (Existing API) | backend URL |
The target should be the backend URL you retrieved earlier in the task, which should look something like this:
Click Next.
Leave the Deploy (optional) settings at their defaults, and click Create.
In this task, you add a VerifyAPIKey policy to the API proxy. Any request which does not provide a valid API key will be rejected.
A VerifyAPIKey policy enforces verification of API keys at runtime, letting only applications with approved API keys to access the API. The policy ensures that the API key is valid, has not been revoked, and is approved to consume the specific resource that is being requested.
Click the Develop tab.
In the Navigator menu for the proxy, in the Proxy Endpoints section, click PreFlow.
The VerifyAPIKey policy should be placed very early on in the API proxy. The request PreFlow in the default proxy endpoint is the first flow that is executed when a request comes in to the API proxy.
In the Flow pane, click the + Add Policy Step button in the upper right above the request flow.
Select Create new policy and under select policy in the Security section, select Verify API Key, and then set the Display Name and Name to VAK-VerifyKey
.
Click Add.
The VerifyAPIKey configuration is shown in the Code pane under Policies.
The APIKey element indicates where the API key is provided in the request.
In the APIKey element, replace request.queryparam.apikey
with request.header.apikey
.
An API key is less likely to be logged or saved in browser history if it is specified in a header.
The backend service is deployed to require authenticated access, so you cannot call the service without a valid OpenID Connect identity token.
The HTTPTargetConnection specifies the backend target for the service.
In the Navigator menu for the proxy, in the Target Endpoints section, click PreFlow.
Find the following code (your URL will be different):
In Cloud Shell, paste and run the following set of commands:
This series of commands uses the Apigee API to determine when the Apigee runtime instance has been created and the eval environment has been attached.
Wait until the instance is ready.
When the text ***ORG IS READY TO USE***
is displayed, the instance is ready. The Apigee organization (org) may have been created before you started the lab, so you might not have to wait for the instance to be created.
If you are waiting for the org to be ready, you can learn about API products, CORS (cross-origin resource sharing), and developer portals.
On the Apigee navigation menu, select Proxy development > API proxies, and then click bank-v1.
Click Deploy.
For Environment, select eval.
For Service account, specify the service account's email address:
Click Deploy, and then click Confirm.
Wait for the eval deployment status to show that the proxy has been deployed.
Click Check my progress to verify the objective.
The eval environment in the Apigee organization can be called using the hostname eval.example.com. The DNS entry for this hostname has been created within your project, and it resolves to the IP address of the Apigee runtime instance. This DNS entry has been created in a private zone, which means it is only visible on the internal network.
Cloud Shell does not reside on the internal network, so Cloud Shell commands cannot resolve this DNS entry. A virtual machine (VM) within your organization can access the private zone DNS. A virtual machine named apigeex-test-vm was automatically created. You can use this machine to call the API proxy.
In Cloud Shell, open an SSH connection to your test VM:
If asked to authorize, click Authorize.
For each question asked in the Cloud Shell, click Enter or Return to specify the default input.
Your logged in identity is the owner of the project, so SSH to this machine is allowed.
Your Cloud Shell session is now running inside the VM.
Call the deployed bank-v1 API proxy in the eval environment:
The -k
option tells curl
to skip verification of the TLS certificate. the Apigee runtime in this lab is using a self-signed certificate instead of a certificate that has been created by a trusted certificate authority (CA).
-k
option to bypass certificate verification for production use cases.
This API attempts to retrieve a list of customers. You should now see a 401 Unauthorized response similar to this:
This response indicates that the API proxy has blocked access to the backend service because the API key was not provided.
Enter the command exit
to leave the SSH session and return to Cloud Shell.
In this task, you add API products that will provide different levels of access for your API. You will then create two applications and associate separate API products for them, providing them with different access.
The first API product provides full access to the service.
On the Apigee navigation menu, select Distribution > API Products.
To create a new API product, click +Create.
In the Product details pane, specify the following:
Property | Value |
---|---|
Name | bank-fullaccess |
Display Name | bank (full access) |
Description | allows full access to bank API |
Environment | select eval |
Access | select Public |
Leave Automatically approve access requests selected.
In the Operations section, click +Add an Operation.
Operations are used to specify which requests in which API proxies are allowed for an application associated with the API product.
Specify the following:
Name | Value |
---|---|
Source | select the bank-v1 API proxy |
Path | /** |
Methods | select GET, PATCH, POST, PUT, and DELETE |
The path expression "/**" indicates that any path suffix of any depth is a match for the operation.
In a production environment, you might choose to add each operation that is allowed separately, rather than using this wildcard path expression.
Click Save to save the operation.
In the Custom Attributes section for the API product, click +Add Custom Attribute.
Custom attributes can be used to attach any data that you would like to be available in the proxy to control access.
In this case, because this is the full access API product for your retail API, you'll create a custom attribute that indicates that the application calling the API should be allowed full access.
Specify the following:
Property | Value |
---|---|
Name | full-access |
Value | yes |
Click OK to save the custom attribute.
To save the API product, click Save at the top of the page.
Return to the Distribution > API Products page. The API product will be listed.
Click Check my progress to verify the objective.
The second API product will provide read-only access to the service.
To create a new API product, click +Create.
In the Product details pane, specify the following:
Property | Value |
---|---|
Name | bank-readonly |
Display Name | bank (read-only) |
Description | allows read-only access to bank API |
Environment | select eval |
Access | select Public |
Leave Automatically approve access requests selected.
In the Operations section, click +Add an Operation.
Specify the following:
Property | Value |
---|---|
Source | select the bank-v1 API proxy |
Path | /** |
Methods | select GET |
Click Save to save the operation.
To save the API product, click Save at the top of the page.
Return to the Distribution > API Products page. The API product will be listed.
Before creating an app, you must create an app developer.
On the Apigee navigation menu, click Distribution > Developers.
To create a new app developer, click CREATE.
Specify the following:
Property | Value |
---|---|
First Name | Joe |
Last Name | Developer |
joe@example.com | |
Username | joe |
Click ADD to create the app developer.
On the Apigee navigation menu, click Distribution > Apps.
To create a new app, click CREATE.
In the App details pane, specify the following:
Property | Value |
---|---|
Name | readonly-app |
Developer | select Joe Developer |
In the Credentials pane, click Add product, click on bank (read-only), and then click Add(1) to add.
Under Product click on checkbox beside bank (read-only) and click on APPROVE
Click Create to create the app.
The Key and Secret are now configured for the app.
On the Apigee navigation menu, click Distribution > Apps > readonly-app.
Under the Credentials click on Show next to Key.
This is the API key which will be used to call the API. You could copy the key, but the next steps will use a curl call to the Apigee API to retrieve the API key.
On the left navigation menu, click Distribution > Apps.
To create a new app, click +App.
In the App details pane, specify the following:
Property | Value |
---|---|
Name | fullaccess-app |
Developer | select Joe Developer |
In the Credentials pane, click Add product, click on bank (full access), and then click Add(1) to add.
Under Product click on checkbox beside bank (full-access) and click on APPROVE
Click Create in the upper right corner to create the app.
On the Apigee navigation menu, click Distribution > Apps > fullaccess-app.
Under the Credentials click on Show next to Key.
Click Check my progress to verify the objective.
In Cloud Shell, open an SSH connection to your test VM:
If asked to authorize, click Authorize.
Your Cloud Shell session is now running inside the VM.
To get the API key for the read-only application, run the following commands:
The first command reads the gcloud configuration to get the current project. The second command retrieves the API key using the Apigee API. The request is authorized because you send an access token that has the permissions of the logged in user.
Call the deployed bank-v1 API proxy in the eval environment, using a fake API key:
The response is 401 Unauthorized with a fault indicating that the API key was invalid.
Call the deployed bank-v1 API proxy in the eval environment, using the real API key:
The request is allowed, and the response contains a list of customers.
Make another call to the deployed bank-v1 API proxy in the eval environment, again using the real API key:
This time the response is 401 Unauthorized with a fault indicating that the API key is invalid for the given resource. The request was rejected because the supplied API key is associated with the read-only API product, which does not allow POST requests.
Enter exit
to exit the virtual machine SSH session.
In this task, you add a Quota policy that will limit the number of requests that will be allowed per application over a specific period of time. The Quota policy will use a quota configuration that has been specified in the API products.
In the Apigee navigation menu, select Proxy development > API Proxies, and then click bank-v1.
Click the Develop tab.
In the Navigator menu for the proxy, in the Proxy Endpoints section, click PreFlow.
The Quota policy will verify that the quota for a specific application has not been exceeded. If the limit has been reached, the Quota policy will raise a fault, and the request will be aborted. If the limit has not been reached, the number of allowed requests will be decremented.
The Quota policy determines the calling application based on a variable populated by the VerifyAPIKey policy. Therefore, the Quota policy must be placed after the VerifyAPIKey policy.
In the Flow pane, click the + Add Policy Step button in the upper right above the request flow.
Select Create new policy and under select policy in the Traffic Management section, select Quota, and then set the Display Name and Name to Q-EnforceQuota
.
Click Add.
The Quota configuration is shown in the Code pane.
Change the Quota configuration to:
You will be using the API product to specify the allowed rate. The stepName in the UseQuotaConfigInAPIProduct element specifies which step will determine the API product.
When an API key or OAuth token is validated, it can be associated with an app that is associated with an API product. Using these policy settings, the VerifyAPIKey step called VAK-VerifyKey determines the API product. The VerifyAPIKey policy must run before the Q-EnforceQuota policy.
The default configuration values specified in the Quota policy specify a maximum of 2 (Allow) requests per 1 (Interval) month (TimeUnit). The default values will only be used if the values are not available, which should only happen if the quota settings are not set for the API product associated with the API key.
Click Save. If you are notified that the proxy was saved as a new revision, click OK.
Click Deploy
For Service account, specify the service account's email address:
click Deploy.
Click the Overview tab, and wait for the eval deployment status to show that the proxy has been deployed.
Click Check my progress to verify the objective.
Debug is a tool for troubleshooting and monitoring API proxies running on Apigee. The Debug tool lets you examine the details of each step during an API call.
On the Apigee navigation menu, select Proxy development > API Proxies, and then click bank-v1.
Click the Debug tab.
In the Start a debug session pane, on the environment dropdown, select eval.
Click Start Debug Session.
It may take a short period of time before the debug session starts capturing requests.
You will make API requests and then examine the debug session.
In Cloud Shell, open an SSH connection to your test VM:
If asked to authorize, click Authorize.
Your Cloud Shell session is now running inside the VM.
To get both API keys, run the following commands:
Repeatedly send this request until you get a quota failure:
Your quota violation will look similar to this:
Return to the Apigee UI tab.
You should see some 200 requests and a 429 request.
Click on a 200 request. The transaction map shows a factory icon at the right side, indicating that the backend was called.
In the Debug session pane, click the left arrow (<) at the upper left to return to the Start a debug session pane.
Click Start Debug Session to start a new Debug session.
Return to the SSH session, and then repeatedly send this request using the full access API key until you get a quota failure:
This time you should be able to send at least 5 requests before being rejected with a 429 response. The quota for the full access API product is 5 requests per minute. The quota is reset when the seconds part of the time resets to zero. The command above prints the time before calling the API, so you should be able to see the approximate time when the quota is reset.
Enter exit
to exit the virtual machine SSH session.
Return to the Apigee UI tab.
If you select a request and click on the quota icon, you can see that the VAK-VerifyKey quota variables now indicate 5 requests per 1 minute.
In this task, you add CORS (cross-origin resource sharing) to the bank-v1 proxy.
CORS is a protocol that uses HTTP headers to indicate to browsers whether it is safe to access restricted resources from a separate domain. By default, cross-domain requests are forbidden by the same-origin security policy. The same-origin policy protects browser users from unknowingly sharing session information with bad actors.
The same-origin policy means that a web page served from www.example.com could not, by default, make a call to APIs at api.example.com because the host name is different. CORS can be used to allow this kind of cross-origin access.
You will need CORS in the bank API for the developer portal. An Apigee developer portal has a domain name of *.apigee.io, and the API is accessed via a different domain. In order to invoke the API from the documentation, you will add CORS headers to all API responses, including error responses.
CORS also uses preflight requests. The browser sends a preflight request using the OPTIONS verb to find out whether the next call will be allowed.
The CORS policy can handle all of the CORS functionality.
For more information about CORS, refer to the Apigee CORS documentation.
On the Apigee navigation menu, select Proxy development > API Proxies, and then click bank-v1.
Click the Develop tab.
In the Navigator menu for the proxy, in the Proxy Endpoints section, click PreFlow.
In the Flow pane, click the + Add Policy Step button in the upper right above the request flow.
Select Create new policy and under select policy in the Security section, select CORS, and then set the Display Name and Name to CORS
.
Click Add.
The CORS policy configuration is shown below the Flow pane.
AllowOrigins lists the allowed origins. The default configuration allows any Origin, because it sets the allowed origin equal to the Origin that is passed in the request. In a typical production use case, you might only allow requests from specific hostnames.
AllowMethods specifies the methods that should be allowed for the API.
AllowHeaders lists the headers that may be passed in the request.
ExposeHeaders specifies the headers in the response that should be allowed when being called with an Origin. With the default value of *, no response headers will be stripped from the response.
MaxAge specifies how long a preflight response may be cached by a browser, in seconds.
AllowCredentials indicates whether Authorization headers, TLS client certificates, or cookies can be sent in the request.
GeneratePreflightResponse specifies whether preflight requests with the OPTIONS method will be handled.
Replace the AllowHeaders configuration with:
Your API is using the apikey
header to specify an API key, so it must be added so it can be called from a browser.
Replace the MaxAge value with -1
.
This will disable the browser's caching of the preflight response, so that you will always see the preflight request. In a production use case, you will typically allow caching of the response to avoid making two calls per request.
In the Navigator menu for the proxy, in the Proxy Endpoints section, click PreFlow.
When the CORS policy was added, it was automatically added at the end of the flow. However, for preflight requests, an API key should not be required.
Move the CORS policy before the VAK-VerifyKey policy by editing the PreFlow configuration. Replace:
with:
Click Save. If you are notified that the proxy was saved as a new revision, click OK.
Click Deploy
For Service account, specify the service account's email address:
click Deploy.
Click the Overview tab, and wait for the eval deployment status to show that the proxy has been deployed.
Click Check my progress to verify the objective.
Click the Debug tab
In the Start a debug session pane, on the environment dropdown, select eval.
Click Start Debug Session.
It may take a short period of time before the debug session starts capturing requests.
In Cloud Shell, open an SSH connection to your test VM:
If asked to authorize, click Authorize.
Your Cloud Shell session is now running inside the VM.
To get an API key, run the following commands:
Make a request to retrieve the list of customers:
There is no Origin, so the CORS functionality is skipped. Confirm that the Access-Control-Allow-Origin header is not returned.
Make another request, but include the Origin header this time. This tests a normal CORS request:
The access-control-* headers are returned because the Origin was supplied.
Make a preflight request:
The CORS policy sets the preflight headers in the response and blocks the request from continuing through to the backend.
If you return to the Apigee UI and look at the OPTIONS call in the Debug tool, you can confirm that the CORS policy did not allow the call to pass through to the backend service.
Enter exit
to exit the virtual machine SSH session.
In this task, you download and modify an OpenAPI specification that defines the interface for your API proxy.
The OpenAPI specification will be used when publishing your API proxy to your developer portal.
In Cloud Shell, to download the OpenAPI specification for your API proxy, run this command:
This curl command downloads a file named simplebank-spec.yaml and stores it in a file with the same name in the home directory.
In Cloud Shell, click Open Editor, and then click Open in new window if necessary.
In the editor, select the simplebank-spec.yaml file.
This OpenAPI specification specifies the interface of the API proxy that you have created during this lab. The specification will be used to provide live documentation in the developer portal.
The developer portal accesses the Apigee proxy from the external network. The hostname you have been using, eval.example.com, is only available on the internal network.
A load balancer has been provisioned to provide external access to the API proxy. External access uses a hostname provided by nip.io, which is a wildcard DNS provider.
The external hostname will look similar to this:
This hostname has already been specified as a matching hostname for the eval-group environment group.
In Cloud Shell, to retrieve the eval-group settings, use the following command:
The hostnames array contains two hostnames: one without an IP address (eval.example.com), and one with an IP address (similar to eval.60.70.80.90.nip.io). You will use the hostname with the IP address in the OpenAPI spec.
In the editor, on line 10, replace the hostname:
Once you have replaced the hostname in the server URL, line 10 should look similar to this:
Click File > Save.
Click File > Download.
This will download the file to your local machine. You will use the updated specification with the developer portal.
In this task, you create an integrated developer portal and then publish your API to it.
In the Apigee navigation menu, select Distribution > Portals and click on GO TO CLASSIC APIGEE UI.
Click Get started.
Enter bank as the name, and then click Create.
Creation may take a minute, and then the portal overview page should open.
If a message to "Enroll in beta for team and audience management features" is displayed, click Enroll.
Click API catalog.
Click +.
Select the bank (full access) product, and then click Next.
Customize the API details:
Property | Value |
---|---|
Published (listed in the catalog) | selected |
Display title | SimpleBank |
Display description | SimpleBank API v1 |
API visibility | select Public (visible to anyone) |
Published makes the API visible on the portal, and Public visibility allows APIs to be seen even if the user is not logged in to the portal.
Click Select image and then click the Image URL link.
Set the image URL to:
You should see the image of a piggy bank.
Click Select.
In the API documentation section, select OpenAPI document.
Click Select Document.
Click on the cloud image to select a file to upload.
Select the OpenAPI spec file you downloaded from Cloud Shell (simplebank-spec.yaml), and then click Open.
Click Select.
Click Save.
To open the developer portal in a new tab, click Live Portal.
Click Check my progress to verify the objective.
In Cloud Shell, to get the full access API key, run the following command:
Copy the API key into the clipboard.
Return to the Live Portal tab, and click APIs.
Click the piggy bank.
The SimpleBank API is shown.
Click Authorize.
Paste the API key as the Key.
Click Authorize, and then click OK.
The API key will now be sent with any request.
In the left menu, click /customers POST.
Specify the following request body:
Click Execute.
A 200 OK response indicates that the customer was created.
In the left menu, click /customers GET.
Click Execute.
The customer you just created is returned with the other customers in the database.
If you return to the Apigee UI and the Debug session, you can see that an OPTIONS (preflight) request was automatically sent by the browser before sending both commands. A preflight request is required before the GET command, since browser does not know whether the apikey header should be allowed.
In this task, you will use the developer portal to create an app developer.
Return to the Live Portal, and click Sign In.
Click Create an account.
Enter a first name, last name, email address, and password.
You will need to use the password to log in to the developer portal, so make it something you can remember.
Click the box indicating that you agree to the terms.
The Terms and Conditions page would be specified by the organization providing the APIs to app developers.
Click Create Account.
An email is sent to your email address. It includes a link that must be clicked to allow the account user to log in.
Click the emailed link.
A browser tab will open to the developer portal.
Click Sign In.
Enter the email address and password, and then click Sign In.
Click the email address in the upper right corner, and then click Apps.
Click + New App.
Specify MyApp
as the App Name.
In the APIs section, for SimpleBank, click Enable.
Click Save.
The app is registered, and the API key is shown. This API key may be used within the developer portal. The app developer and app can be seen by returning to the Apigee UI and navigating to Publish > Developers and Publish > Apps respectively.
In this lab, you used API key verification to restrict access to the API. You created API products to provide different levels of service for internal and external app developers. You used a Quota policy to limit the number of calls for each app, and added a CORS policy to support Cross-Origin Resource Sharing in the API. You created a developer portal and published the API products for app developers to consume.
...helps you make the most of Google Cloud technologies. Our classes include technical skills and best practices to help you get up to speed quickly and continue your learning journey. We offer fundamental to advanced level training, with on-demand, live, and virtual options to suit your busy schedule. Certifications help you validate and prove your skill and expertise in Google Cloud technologies.
Manual Last Updated January 27, 2025
Lab Last Tested January 27, 2025
Copyright 2025 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.
This content is not currently available
We will notify you via email when it becomes available
Great!
We will contact you via email if it becomes available
One lab at a time
Confirm to end all existing labs and start this one