How to add a policy to Apigee Proxy?

Pankaj Sharma
pankajtechblogs
Published in
7 min readJul 10, 2021

--

Welcome back, this is a continuation of my previous blog — https://iampankajsharma.medium.com/how-to-create-apigee-api-proxy-219fa2df1425

On clicking the Edit proxy button. Now we are landed on the User-Service proxy overview.

We can see that it tells details about the Deployments — env, status, and which revision of code is deployed, Proxy Endpoints — defines the base path and which proxy target it connects(by default its default) & Target Endpoints — defines the details of the backend server and in which ProxyEndpoint it is used(by default its default).

Let's click on the Develop tab (listed on the top right corner). And we are landed on the Development view of the proxy, and we notice some XML (in the middle panel) which is the default code and is created by Apigee, based on what we configured over GUI in previous steps.

Let’s understand this view more. It has a Project dropdown Top left corner (as mentioned in the image below)

Clicking on that will show some options (as shown below) to save as a new revision, save as a new API proxy, delete the proxy, download the revision of the proxy, or upload the proxy.

Next to Project dropdown, we have a Revision drop-down, that lets us select the revision from the list of available revisions.

Now let's try to promote this proxy to another environment (Prod) directly using the Apigee UI. That we can do by clicking on Deployment dropdown that is next to Tools dropdown in the same bar. If you notice it shows Green in front of the test environment, which means the selected proxy revision is currently deployed on this particular environment.

Now let's click on Prod, and click Deploy on the popup. And within few seconds it gets promoted to Prod as well.

We can get the prod environment URL by navigating to the overview section, by just looking at the Deployments section. Remember… we discussed the overview tab above. :-)

Let’s navigate back to the Develop tab, by clicking on the Develop tab on the link mentioned at the top right corner as shown in the above image.

Now we will try to add some policies to the Proxy.

Adding Policy to the proxy

A policy is like a module that implements a specific, limited management function. Policies are designed to let you add common types of management capabilities to an API easily and reliably.

It can provide features like security, rate-limiting, transformation, and mediation capabilities, saving you from having to code and maintain this functionality on your own. We are not limited to the set of policies provided by Apigee, but we can write custom Javascript code or Node.JS code as well.

If we notice the response of the API proxy as of now is in JSON format.

The reason is — backend server sends the content type as application/json in response. And we made a pass-through proxy, without doing any transformation to the response.

Now let’s try to convert the response to XML, by doing some Mediation at the proxy layer itself. So, let’s add a policy to convert the JSON to XML.

As shown in the image above, from the Navigator view, we can see Policies, let's click on the + icon for adding a Policy from the list of policies. On the left panel in the code section in the navigator view.

It will show the pop-up(as shown in the below image), with the list of policies that we can select for various features, like authentication or traffic management etc. Let's scroll down and select JSON to XML policy in the Mediation section, and enter Display Name and Name, for the policy and click Add.

  1. Display Name — This name will be used to identify the policy from list of policies in the navigator view.
  2. Name — This name will be used to use the policy in the ProxyEndpoint or TargetEndpoint flows.

We will notice a default policy code is added by Apigee itself. For now, let's keep the same code, and I will explain it in a while.

Notice in the Navigator view — the policy is added in the Policies section.

Let us add it to the API proxy Flow now.

Hey, wait — But which flow will we be exactly adding this in code, is it in request or in response of Preflow or Postflow? And will it be in TargetEndpoint or will it be in ProxyEndpoint?

Since we would like to convert the response for the client, we can do that easily in the RESPONSE section of ProxyEndpoint Postflow, that’s just before the request leaves the Apigee and the response is sent by Apigee to the client. Refer to the flow diagram in the previous blog.

Marked with a Green tick, to show it in the flow diagram.

Let’s add the code in the ProxyEndpoint Postflow section.

We will add the code in the Postflow child tag of the ProxyEndpoint XML tag. It has Request and Response child tags since we are dealing with the response here that we need to send to the client. We will add the code in the Response child tag.

How to link the policy in the flow?

We can do so using the Step tag and mention the name of the policy which we want to invoke using the Name child tag of the Step tag.

Added a Step in the Response tag.

So we added the step in Response flow, which calls the Policy JTX-JsonToXmlConversion defined in Name child tag of Step tag.

Now let’s save the revision by clicking on the save button, and this will automatically deploy the revision to the environments. It might prompt at times to save as a new revision, if it does for you then, we can click on Save as a new revision, and we can deploy the revision manually, as we showed above, using Deployment dropdown, and click on the environment (test or prod in our case) we want to deploy the proxy.

Now let's invoke the Apigee API proxy URL and we will notice the response is converted to XML — http://catallicaws-eval-test.apigee.net/v1/user-service.

JSON to XML conversion at Apigee Gateway itself.

Now let’s see the policy code which we added just now JSON-XML policy.

Now let us see the important tags here for the policy we added, and we can notice those in our response as well.

  1. ObjectRootElementName → What will be the root tag name for converted data. we can set anything here based on the client’s requirements, based on API proxy contract. This we can notice in the response as well.
  2. Source →Same way which objects data we need to convert, this will be response object here(as we get the response back from TargetEndpoint in response object only, request and response are standard Apigee variables).
  3. OutputVariable → What will be the output variable where the converted data need to be set by Apigee. Since we need it in response we set it to response variable here.

Similarly, we can add other policies as well, let refer to the next blog for adding the Assign-Message policy. Stay tuned… :-)

Reference: https://docs.apigee.com/

--

--

I am a tech enthusiast, currently working in the IT industry. I love to explore tech stacks, frameworks, and design/develop MS using various design patterns.