Apigee Analytics Integration

🚧

Preview Feature - Please Use with Caution and Provide Feedback

This feature is currently in preview and may experience issues. Please note that it is still under development and should not be relied upon for critical tasks. Any feedback you can provide will be valuable in improving this feature for future releases. Thank you for your understanding.

This guide walks you through how to integrate Apigee with your Enterprise Hub, allowing you to display usage analytics for the API(s) on Apigee in Studio and the Developer Dashboard.

🚧

By default, logs sent are anonymous

The suggested integration will send API logs from Apigee to Rapid, but will not include the APP ID (consumer) by default. This can be solved by adding consumer logic to the script. Enhancing the solution to include easier consumer integration is a part of our backlog.

Installation

There are two main parts to this installation:

  1. Create an API Project that does not use the Rapid Runtime and has a base URL pointing to Apigee (skip if you already have this).
  2. Forward access logs from Apigee to the Platform Analytics API.

Create an API Project that has a base URL pointing to Apigee

The Platform Analytics API supports using the base URL as an API identifier (instead of the Rapid API ID). However for this to work, the base URL on the API Project listing must match the one being passed in from the gateway.

As an enterprise customer, you can enable external API Gateways through the Admin Panel. For this setup to work, the base URL you add to the API must contain the entire request URL except for the resource path.

Example

Depending on your setup, your API request URL might looks something like this:

https://34.45.72.78.nip.io/myapi/hello

Apigee uses the following structure:

http://{org-name}-{env-name}.apigee.net/{base-path}/{resource-path}

Using the above example, that would mean that:

Base Path = "/myapi"
Resource Path = "/hello"

Given that, the only part you want to include in the base URL field on Rapid is in bold:

https://34.45.72.78.nip.io/myapi/hello

Your gateway code template Request URL field for APIs using Apigee should simply concatenate the Base URL and the path:

Request URL: "{{API_BASE_URL}}{{PATH}}"

1704

Configuring a code template to be used with Apigee.

⚠️ Warning: If you have a custom DNS, your setup might be slightly different. Please contact support if you are not sure how to proceed.

Apigee Setup

This section will walk you through how to forward logs from Apigee to the Platform Analytics API
Apigee is very flexible, and there are many different ways of achieving the same thing. For this integration, we use the Assign Message & JavaScript policies offered by Apigee.

Install the Response PostFlow Javascript Policy on the proxy

We will use a Apigee’s JavaScript policy to send logs to the Platform Analytics API. The first thing to do is create a new JavaScript Resource. The code we will upload will execute with each API call, transform the metadata into the right format for the Platform Analytics API, and call the API.

  1. On Apigee, open a proxy on the develop tab.
  2. Select + on the resources section.
  3. On the 'Add resource' pane, select the 'Javascript' resource type.
  4. For 'Source' select 'Import file' and upload the JavaScript file provided here.
  5. Name your resource (we recommend "ForwardLogsToRapidAPI.js").
2006

Configuring a JavaScript Resource in Apigee.

The next step is to create the actual JavaScript Policy.

  1. Click the + on the Policies section.
  2. Select "Javascript" under the "Select policy type" dropdown.
  3. Add a name and display name for your policy. (we use "JS-ForwardLogsToRapidAPI" in our example)
  4. Click "Create".
2006

Creating the JavaScript Policy in Apigee.

📘

Tip: Create a reusable environment resource

When using the Apigee UI, the resource file created is associated with the API proxy revision. It is possible to programmatically create an environment resource which can then be re-used across all your APIs and revisions.

1. Upload the env file: here is an example of how to do that using Shell:
curl --request POST
--url 'https://apigee.googleapis.com/v1/organizations/{YOU ORG}/environments/{YOUR ENV}/resourcefiles?name=ForwardLogsToRapidAPI.js&type=jsc'
--header 'Authorization: Bearer {GENERATE TOKEN FROM GOOGLE SHELL}'
--header 'Content-type: application/octet-stream'
--header 'X-Goog-User-Project: {YOUR ENV}'
--data '{COPY PASTE THE CONTENTS OF THE JS FILE HERE}
2. Update the name of the ResourceURL in the Apigee Policy XML
jsc://ForwardLogsToRapidAPI.js

Great! You now have a Javascript policy that executes your newly uploaded Javascript code. In the next step, we will configure your proxy to execute this code. The reason we use the Response PostFlow as the context is that it contains critical information we need like the Latency and Response Code.

  1. Select your endpoint under the Proxy endpoints section.
  2. Click the + on the Response PostFlow 'All' object. (If you want to forward logs from all endpoints of the API)
  3. Add the JavaScript policy you created in the step above.
2160

Configuring your proxy to execute the code in Apigee.

Install the Request PreFlow AssignMessage on the proxy

Almost there! If you deployed your proxy now, logs would be forwarded. However, they will miss a critical piece of information needed to process them. In order to know which API on your Enterprise Hub matches with the proxy API, we need the BaseURLs to match. However, by default Apigee's response context object is not aware of the request data. In the next step, we will use the AssignMessage policy to pass the needed context to the response, so our JavaScript policy can use that context to extract the BaseURL as well as HTTP Method used.

  1. Select the + on the Policies section.
  2. Select Assign Message in the Select policy type dropdown.
  3. Add a name and display name (in the example we use "AM-CopyMessage").
  4. Click Create.
  5. Click on the new Assign Message policy.
  6. Update the xml file in the editor with the text provided here.
  7. Click Save.
2160

Assigning a policy in Apigee.

In the final step, we need to assign this new policy to the RequestPreFlow, so that the context it has can be used by our JavaScript policy in the Response PostFlow.

  1. Select your endpoint under the Proxy endpoints section.
  2. Click the + on the Request PreFlow 'All' object.
  3. Add the AssignMessage policy you created in the step above.
2160

Assigning the policy in Apigee.

Add Support for Error Codes

By design, Apigee does not call the postflow steps when an error is returned from the service. In order to also send error responses to Rapid, you will need to add a small XML snippet.

  1. Click on the "Target endpoints" proxy (most likely default)
  2. Add a fault rule that calls the above configured JS policy.
<FaultRules>
    <FaultRule name="default-fault">
      <Step>
        <Name>JS-ForwardLogsToRapidAPI</Name>
        <Condition>fault.name = "ErrorResponseCode"</Condition>
      </Step>
    </FaultRule>
  </FaultRules>

Finishing Up

This Apigee proxy is now configured to forward logs to the Platform Analytics API for each API call it receives. Test this using the following:

  1. Create an API call.
  2. Validate the call was made to the Platform Analytics API(Open the Apps dashboard, select analytics on the App you used and view the logs).
  3. Validate the log is available on your API.

📘

Tip: Automate using Apigee Proxy Bundles

One of the methods for creating and updating Apigee API proxies is by using an API proxy configuration bundle. All the above configurations can be saved within the template once and reused when creating our updating proxy listings.