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
Enable Google Cloud APIs
/ 15
Create a service account
/ 15
Create a Model Armor template
/ 15
Create an API proxy
/ 15
Test the API proxy
/ 15
Update the API proxy error response for user prompts
/ 15
Test the updated error responses from the proxy endpoint
/ 10
Enable Google Cloud APIs
/ 15
Create a service account
/ 15
Create a Model Armor template
/ 15
Create an API proxy
/ 15
Test the API proxy
/ 15
Update the API proxy error response for user prompts
/ 15
Test the updated error responses from the proxy endpoint
/ 10
Apigee is a platform for developing and managing APIs. Apigee can help you leverage Google Cloud services like Gemini Enterprise Agent Platform, Pub/Sub, Cloud Logging, or any other cloud service providing a REST API.
In this lab, you develop an Apigee API proxy to sanitize a user prompt to a Gemini model and sanitize the model's response.
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 right is the Lab setup and access panel with the following:
Note that the lab timer is located near the top of the page, showing the remaining time.
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 setup and access panel.
Click Next.
Copy the Password below and paste it into the Welcome dialog.
You can also find the Password in the Lab setup and access panel.
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.
In this task, you enable the APIs that will be used by the Apigee API proxy using the gcloud command.
In Cloud Shell, set the environment variables:
Enable the required APIs:
Model Armor will be used to screen LLM prompts and responses, and ensure responsible AI practices.
Gemini Enterprise Agent Platform will be used to access a Gemini model for AI inferencing.
Click Check my progress to verify the objective.
In this task, you configure a service account to be used by the Apigee proxy.
apigee-gc-service-access service account and its project-level roles (roles/modelarmor.user, roles/modelarmor.viewer, and roles/aiplatform.user) have been pre-provisioned during lab startup.Verify that the service account is created:
Verify that the service account has the Model Armor User, Model Armor Viewer, and Agent Platform User roles:
To enable the Apigee API proxy to authenticate with services on Google Cloud using Google access tokens, grant the Service Account Token Creator (roles/iam.serviceAccountTokenCreator) role to the Apigee Service Agent on the apigee-gc-service-access service account:
Click Check my progress to verify the objective.
You use Model Armor templates to configure the screening of prompts and responses for your AI applications. A template provides customized filters and thresholds for multiple safety and security categories. For more information, see Model Armor templates.
In Cloud Shell, set an environment variable for the template ID:
Set the Model Armor API endpoint for your region:
Create the template:
A Model Armor template is a reusable configuration that specifies how Model Armor should protect your AI models by filtering user prompts and AI-generated responses against various risks. This command creates a template with filters along with confidence levels that determines how aggressively Model Armor will block content that contains the type specified.
The template enables filters to detect:
Click Check my progress to verify the objective.
In this task, you create the Apigee API proxy using the Apigee Model Armor proxy template.
To open the Apigee console:
Apigee into the Search bar, then select Apigee from the Products & pages results.The Apigee console will open, where you can manage APIs, environments, proxies, and deployments.
In the navigation menu, select Proxy development > API proxies.
To create a new proxy using the proxy wizard, click Create.
For Proxy template, select AI template > Proxy with Model Armor.
The proxy will enforce safe AI interactions with policies that sanitize user prompts and model responses.
For the Proxy details, specify the following:
| Property | Value |
|---|---|
| Proxy name | promptllm-v1 |
| Base path | /promptllm/v1 |
| Target (Existing API) |
/promptllm/v1 for the base path, not /promptllm-v1.Click Next.
Leave the Deploy (optional) settings at their defaults, and click Create.
The Apigee API proxy is created.
To use the SanitizeUserPrompt policy in the proxy, you must provide it the location of the content in the API request.
To update the default policy, click the Develop tab in the Proxy Editor.
In the navigation menu in the Apigee UI, select Policies > Sanitize User Prompt.
In the SUP-Sanitize-User-Prompt.xml file, replace the entire <SanitizeUserPrompt> element with:
The updated policy provides the location of the Model Armor template that you created earlier in the lab and provides the location of the content in the request (the prompt) that will be evaluated.
jsonPath() is a function in Apigee that is used to select or extract data from a JSON message payload or JSON variable. In this case, it returns the value of the text field in all the elements of the parts[] array within each element of the contents[] array from the request message sent to the API proxy. This JSON structure is required to send the input prompt to the Gemini model.To save the changes to the policy, click Save.
The SanitizeModelResponse policy is used in conjunction with the SanitizeUserPrompt policy. The policy uses Model Armor to evaluate model responses for harmful or offensive content and protects AI clients and workloads using the Apigee API proxy.
Note that although the SanitizeUserPrompt policy is added to the API proxy endpoint request, the SanitizeModelResponse policy is added to the API target endpoint response. To use the policy, you must provide it the location of the content in the target response and the Model Armor template.
To update the default policy, click the Develop tab in the Proxy Editor.
In the navigation menu in the Apigee UI, select Policies > Sanitize Model Response.
In the SMR-Sanitize-Model-Response.xml file, replace the entire <SanitizeModelResponse> element with:
The updated policy provides the location of the Model Armor template that you created earlier in the lab, the location of the prompt content in the request, and the location of the response content returned by the model.
To save the changes to the policy, click Save.
The <HTTPTargetConnection>/<Authentication> element in the API proxy TargetEndpoint is used to make authenticated calls to the model endpoint running on Google Cloud. Apigee uses the service account you created earlier in the lab to generate the necessary token required to securely call the endpoint.
Click the Develop tab in the Proxy Editor.
In the Navigator menu for the proxy, in the Target endpoints section, click default > PreFlow.
In the default.xml file, find the following code (your URL will be slightly different):
<HTTPTargetConnection> section, confirm that you clicked PreFlow in the Target endpoints section, and not in the Proxy endpoints section.Above the <URL> element, add this <Authentication> element:
Your TargetEndpoint code should now look similar to this:
Click Save.
Click Check my progress to verify the objective.
In Cloud Shell, paste and run the following command:
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 explore the AI products and services on Google Cloud.
Return to the promptllm-v1 API proxy in the Apigee UI, and click the Develop tab.
Click Deploy.
For Environment, select eval.
For Service Account, specify the service account's email address:
Click Deploy.
In the confirmation dialog, click Confirm.
Click the Overview tab, and wait until the eval deployment status indicates that the proxy is deployed.
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 project 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 Do you want to continue (Y/n)?, type Y.
For each question asked in Cloud Shell, press ENTER 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 promptllm-v1 API proxy in the eval environment:
contents array contains the conversation or prompt in the text field of the parts array, and the generationConfig element requests that the model return only one generated response or candidate.The response from the API looks like this:
The fault error message contains the URIMatchesFound: true filter match that correctly indicates the presence of a malicious URL in the input prompt.
Send another prompt without any malicious content to the API:
The request to the model is not blocked, and the model response should resemble this:
Type exit and press ENTER to leave the SSH session and return to Cloud Shell.
Click Check my progress to verify the objective.
While testing the API proxy with malicious input in the request prompt, the proxy responds with the default fault error message that was generated when the Model Armor template filter matched the offending text.
In this task, you update the API proxy to return custom error responses when a bad user prompt is detected. The error response format is made consistent with the response returned from the model in the success scenario when no harmful text is detected in the user prompt.
The Apigee RaiseFault policy lets you define a custom response that is returned to the requesting app when a specific error condition arises.
You'll create a RaiseFault policy for the malicious URL filter match condition configured for the Model Armor template.
In the Apigee UI, navigate to the Develop tab, and click Policies > Add Policy.
For Select Policy, to filter the policies, type Raise Fault.
Select Mediation > Raise Fault.
In the Details section, for Name, type:
For Display name, type the same value RF-MaliciousURL-Detected.
Click Create.
In the RF-MaliciousURL-Detected.xml file, replace the entire <RaiseFault> element with:
This RaiseFault policy alters the default error response payload content and status code that is returned if a malicious URL is detected in the input request.
You could also create additional RaiseFault policies to match the other filter match conditions. In this lab, you will create a generic RaiseFault policy that rewrites the error for all other filter match conditions.
To create the generic RaiseFault policy, on the Develop tab, click Policies > Add Policy.
For Select Policy, to filter the policies, type Raise Fault.
Select Mediation > Raise Fault.
In the Details section, for Name, type:
For Display name, type the same value, RF-OtherError-Detected.
Click Create.
In the RF-OtherError-Detected.xml file, replace the entire <RaiseFault> element with:
In the next subtask, you'll add these policies to the proxy and use specific conditions to trigger them.
In the Apigee UI, on the Develop tab, select Proxy endpoints > default > PreFlow.
In the PreFlow, click Add Policy Step.
For Select existing policy, select Mediation > RF-MaliciousURL-Detected.
For Condition, type:
Click Add.
Repeat the steps above to add an additional step for the RF-OtherError-Detected RaiseFault policy with this condition:
After these policies are added, the ProxyEndpoint > PreFlow > Request xml should look like:
The RaiseFault policy RF-MaliciousURL-Detected is executed after the SanitizeUserPrompt policy SUP-Sanitize-User-Prompt if the specified condition evaluates to true. The SanitizeUserPrompt policy sets this flow variable for the malicious URL filter match state to MATCH_FOUND or NO_MATCH_FOUND depending on the content in the prompt.
The RaiseFault policy RF-OtherError-Detected will execute when any of the other filter conditions match.
To allow the proxy execution flow to reach the RaiseFault policies when the SanitizeUserPrompt policy returns an error, set the continueOnError attribute of this policy to true.
To do this, click Policies > Sanitize User Prompt.
In the SUP-Sanitize-User-Prompt.xml file, on line 2, set the value of the continueOnError attribute to true.
To save the changes to the proxy, click Save. If prompted to save the proxy as a new revision, click Save as new revision.
Click Check my progress to verify the objective.
To deploy the new proxy revision, click Deploy.
For Environment, select eval.
For Service Account, specify the service account's email address:
Click Deploy.
In the confirmation dialog, click Confirm.
Click the Overview tab, and wait until the eval deployment status indicates that the proxy is deployed.
In Cloud Shell, open an SSH connection to your test VM:
If asked Do you want to continue (Y/n)?, type Y.
For each question asked in Cloud Shell, press ENTER 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 promptllm-v1 API proxy in the eval environment:
The response from the API should now resemble:
Repeat the call with a prompt that contains sensitive information:
The request content matches the sensitive data protection filter, which triggers the generic RaiseFault policy RF-OtherError-Detected in the API proxy that returns the error response:
Click Check my progress to verify the objective.
In this optional task, you create another RaiseFault policy in the API proxy to return error responses when harmful content is detected in the model response. The error response format is made consistent with the response returned from the model in the success scenario when the model response does not contain harmful content.
To complete this subtask, follow option 1 or option 2.
Option 1: Create a RaiseFault policy named RF-SDP-Detected.
Option 2: Full solution (expand to see the steps)
After the policy is created using either option 1 or 2, replace the entire <RaiseFault> XML element in the RF-SDP-Detected.xml file:
To complete this subtask, follow option 1 or option 2.
Option 1: Add the RaiseFault policy RF-SDP-Detected to the API target endpoint.
Option 2: Full solution (expand to see the steps)
Repeat the steps above to add an additional policy step for the RF-OtherError-Detected RaiseFault policy with this condition:
These RaiseFault policies are executed after the SanitizeModelResponse policy SMR-Sanitize-Model-Response if the specified condition evaluates to true.
To allow the proxy execution flow to reach the RaiseFault policies when the SanitizeModelResponse policy returns an error, set the continueOnError attribute of this policy to true.
To do this, click Policies > Sanitize Model Response.
In the SMR-Sanitize-Model-Response.xml file, on line 2, set the value of the continueOnError attribute to true.
To save the changes to the proxy, click Save.
To test the updated error responses from the target endpoint, you must override the successful model response to inject a bad message in the response payload. This is because in order for the execution flow to reach the target endpoint, the API request must reach the model successfully with a valid user prompt that generates a valid response from the model.
In the Apigee UI, for the promptllm-v1 API proxy, click the Develop tab.
In the Apigee navigation UI, click Target endpoints > default > PreFlow.
In the PreFlow, click Add Policy Step.
Select Create new policy, and click Mediation > Assign Message.
For the policy name, type AM-InjectBadResponse. Provide the same value for Display name.
Because this policy should only be executed for testing purposes, add a condition that checks for the presence of a header in the API request.
To implement this step, follow option 1 or option 2:
Option 1: Review the documentation.
Option 2: Specify the condition.
Later, when you test the proxy, you pass in this header in the request using curl. Apigee creates a flow variable with the name of the header as specified above with the value passed in the request.
Click Add.
In the Policies section, click the AM-InjectBadResponse policy, and replace the entire <AssignMessage> element in the AM-InjectBadResponse.xml file with this code:
In the Target Endpoint: default graphical UI pane, in PreFlow, select the AM-InjectBadResponse policy and drag it up so that it is the first policy to execute in the flow.
The <TargetEndpoint>/<PreFlow> xml element should look like:
To save the changes to the proxy, click Save. If prompted to save the proxy as a new revision, click Save as new revision.
To deploy the new proxy revision, click Deploy.
For Environment, select eval.
For Service Account, specify the service account's email address:
Click Deploy.
In the confirmation dialog, click Confirm.
Click the Overview tab, and wait until the eval deployment status indicates that the proxy is deployed.
In Cloud Shell, open an SSH connection to your test VM:
If asked Do you want to continue (Y/n)?, type Y.
For each question asked in Cloud Shell, press ENTER 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 promptllm-v1 API proxy in the eval environment (without the test request header):
The response from the API contains a successful response from the model.
Call the API proxy again, this time supplying the test header in the request:
The response from the API contains the error response from the RF-SDP-Detected RaiseFault policy which was triggered because of the sensitive data protection filter that matched the content in the response.
In this lab, you:
Manual Last Updated on November 6, 2026
Manual last tested on September 16, 2026
Copyright 2026 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 can notify you via email when it becomes available
Great!
We can contact you via email if it becomes available
One lab at a time
Confirm to end all existing labs and start this one