---
schema_version: "1"
id: c3dbc1f9f64a5d92bf441c46a22dbf13c87f3fbbe51652b5a1f85a275f763008
title: Tracking Requests with Autodiscovery
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/anypoint-code-builder/int-autodiscovery-config.md
canonical_url: https://docs.mulesoft.com/anypoint-code-builder/int-autodiscovery-config
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 725b0c2af66e1ebf4c2c8ef190714aead41413df956e2575c20a3a639ed7536a
description: MuleSoft Documentation Site
latest: true
---
---
product: Anypoint Code Builder
version: master
---

# Tracking Requests with Autodiscovery

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

Set up API autodiscovery in a Mule app to display key metrics in API Manager, including requests to
endpoints in the app. API Manager is part of Anypoint Platform.

![View request metrics for a Mule app in API Manager](/mulesoft/current/en-us/anypoint-code-builder/_images/autodiscovery-data-apim.png)

Autodiscovery configuration in a Mule app requires an API instance ID from an API in API Manager
that runs on a Mule Gateway runtime, and your app requires Anypoint Platform credentials (client ID
and secret) for connecting to your API in API Manager.

For other important uses of Mule Gateway API instances, review
[Managing Instances](https://docs.mulesoft.com/api-manager/latest/api-instance-landing-page) and
[Mule Gateway Policies Overview](https://docs.mulesoft.com/mule-gateway/policies-policy-overview).

## Before You Begin

To obtain the API instance ID for your Mule app’s autodiscovery configuration, you must have access
to an API in API Manager that runs on a Mule Gateway runtime, or you can create your API by
following the procedure in
[Adding a Mule Gateway API Instance](https://docs.mulesoft.com/api-manager/latest/create-instance-task-mule), in the API
Manager documentation.

APIs in API Manager are associated with an API spec on Anypoint Exchange that defines endpoints in
your Mule app. For an API spec example that defines an endpoint and a Mule app example that
implements the endpoint, see [Examples](#examples). For guidance creating API specs, see:

- [Creating and Importing API Specifications](https://docs.mulesoft.com/anypoint-code-builder/des-create-api-specs) to design a spec from Anypoint
  Code Builder

- [Designing API Specs and Fragments](https://docs.mulesoft.com/design-center/design-designing-api-specs-fragments) to
  design a spec from Design Center

For guidance publishing an API spec to Exchange, see:

- [Publishing API Projects to Exchange](https://docs.mulesoft.com/anypoint-code-builder/des-publish-api-spec-to-exchange) to publish your spec from
  Anypoint Code Builder

- [Publishing API Specifications](https://docs.mulesoft.com/design-center/design-publish) to publish your spec from Design
  Center

## Set Up API Autodiscovery in a Mule App

Configure a Mule app to send request data to an API running on a Mule Gateway runtime in API
Manager. The API tracks this data in charts on API Manager.

Autodiscovery configuration of a Mule app requires a global element (`<api-gateway:discovery/>`)
that references an API instance ID along with credentials (client ID and secret) that enable the
Mule runtime engine in your Mule app to connect to the Anypoint Platform business group or
environment for your API in API Manager.

To set up autodiscovery:

1.  Meet the prerequisites in [Before You Begin](#prereqs).

2.  Obtain the client ID and secret for your business group from Access Management
    ([US](https://anypoint.mulesoft.com/accounts/businessGroups/),
    [EU](https://eu1.anypoint.mulesoft.com/accounts/businessGroups)), in Anypoint Platform.

    Show me how

    1.  Open the list of business groups in Access Management, on Anypoint Platform
        ([US](https://anypoint.mulesoft.com/accounts/businessGroups/),
        [EU](https://eu1.anypoint.mulesoft.com/accounts/businessGroups)).

        To navigate to the business group list from the Anypoint Platform home page instead, open
        Access Management, and click **Business Groups**.

    2.  In the business group list, click the name of your business group.

    3.  Click the **Settings** tab for your business group.

    4.  Obtain the **Client ID** and **Client Secrets** values from the **Settings** tab in Access
        Manager.

3.  Obtain the API instance ID from your API instance in API Manager.

    Show me how

    1.  In Anypoint Platform ([US](https://anypoint.mulesoft.com/),
        [EU](https://eu1.anypoint.mulesoft.com/)), navigate to API Manager.

    2.  In the **API Administration** page of API Manager, locate and click the name of your API to
        open its **API Summary** page.

    3.  Find the **API Instance ID** in the **API Summary** page.

        You’ll need the instance ID when configuring your Mule app for autodiscovery.

4.  In your Mule app in Anypoint Code Builder, use your API instance ID to pair your app to the API
    instance:

    You can add API Autodiscovery from **Global Elements** instead of editing XML. See
    [Add a Global Configuration](https://docs.mulesoft.com/anypoint-code-builder/int-global-config-elements#add-global-config). .. In the code
    editor for your Mule app, add the `<api-gateway:autodiscovery/>` element above `<flow/>`
    elements in the app, for example:

    -

    ```xml
    <api-gateway:autodiscovery apiId="YOUR_API_ID_HERE" flowRef="currentTimeFlow" doc:name="Autodiscovery" />
    ```

    \+ For more complete context, see the example in [Example: Mule Application](#mule-app).

    1.  Paste your API instance ID as the `apiID` value, for example:

        ```xml
        <api-gateway:autodiscovery apiId="123456" flowRef="currentTimeFlow" doc:name="Autodiscovery" />
        ```

5.  In Anypoint Code Builder, append the client ID and client secret for your business group to the
    Mule runtime arguments in the **Settings** tab:

    1.  Open the **Settings** tab.

        Show me how

        - From the Anypoint Code Builder panel, click the ![](/mulesoft/current/en-us/anypoint-code-builder/_images/icon-settings-gear.png) (gear)
          icon.

        - From the activity bar, click the ![](/mulesoft/current/en-us/anypoint-code-builder/_images/icon-settings-gear.png) (gear) icon, select
          **Settings**, and search for `mule` in the **Settings** tab.

        - Provide the command `MuleSoft: Open ACB Settings` in the
          [Command Palette](https://docs.mulesoft.com/anypoint-code-builder/start-use-command-palette).

    2.  In the **Settings** tab, find **Mule › Runtime: Default Arguments**.

    3.  In the text field, append your client ID and secret arguments to the end of the existing
        arguments, for example:

        \-M-Danypoint.platform.client\_id=999abcde1f888e9h9aaa00ff5a5b03c5
        -M-Danypoint.platform.client\_secret=abc0d1234e5a4ab321A000B2033D4E56

        Replace the mock values in the example with your client ID and secret.

6.  Run your application to ensure that the API is connected by checking the build logs for the
    following information:

    - The client ID and client secret validated successfully against API Manager.

    - The underlying `GateKeeper` is available (unblocked) and in `FLEXIBLE` mode.

    - The API Gateway extension is in `STANDALONE` mode.

7.  Send requests to an endpoint in your app that is tracked by your API instance.

    For guidance, see [Triggering Flows in Your Development Environment](https://docs.mulesoft.com/anypoint-code-builder/int-trigger-flows).

8.  In API Manager, check that the API instance is now registered (active) and produces metrics in
    API Manager.

    ![API in API Manager with autodiscovery data](/mulesoft/current/en-us/anypoint-code-builder/_images/autodiscovery-apim-fullview.png)

### Examples

Examples tested for API autodiscovery include the following:

- [Example: OAS Spec](#oas-spec):

  Testing involved creating the spec in Design Center, publishing the spec to Exchange, creating an
  Mule Gateway API instance in API Manager that is associated with the spec on Exchange.

- [Example: Mule Application](#mule-app)

  Testing involved adding the `<api-gateway:autodiscovery/>` element with the `apiID` value from API
  Manager, appending the client ID and secret to the Mule runtime arguments in Anypoint Code Builder
  settings, running the app in debug mode to deploy the app locally, making requests to the defined
  endpoint in the app, and checking that the associated Mule Gateway API instance on API Manager is
  **Active** and that the requests produce data (key metrics) in the instance’s charts.

#### Example: OAS Spec

The following OAS spec defines a single endpoint in the [Mule app example](#mule-app).

```yaml
openapi: "3.0.0" (1)
info:
  version: 1.0.0
  title: my-autodiscovery-api
servers:
  - url: https://my-autodiscovery-app.us-e2.cloudhub.io
paths:
  /current-time: (2)
    get:
      responses:
        "200":
          description: my description here.
          content:
            application/json:
              schema:
                type: object
                properties:
                  current-time:
                    type: string
              example:
                current-time: "2024-08-05 14:56:12"  (3)
```

<table><tbody><tr><td><i data-value="1"></i><b>1</b></td><td>OAS 3.0 specification</td></tr><tr><td><i data-value="2"></i><b>2</b></td><td><code>/current-time</code> endpoint definition</td></tr><tr><td><i data-value="3"></i><b>3</b></td><td>Formatted example of a date-time value</td></tr></tbody></table>

#### Example: Mule Application

```xml
  <http:listener-config name="HTTP_Listener_config" > (1)
    <http:listener-connection host="0.0.0.0" port="8081" />
  </http:listener-config>

<api-gateway:autodiscovery apiId="YOUR_API_ID_HERE" flowRef="currentTimeFlow" doc:name="Autodiscovery" /> (2)

  <flow name="currentTimeFlow"> (3)
        <http:listener path="/current-time" config-ref="HTTP_Listener_config" doc:name="Listener" doc:id="bce0d0"/> (4)
        <ee:transform doc:name="Transform" doc:id="ahlupg" > (5)
            <ee:message>
                <ee:set-payload >
                <![CDATA[#[%dw 2.0
output application/json
---
{
	"current-time": now() as String {format: "yyyy-MM-dd hh:mm:ss"}
}]]]>
            </ee:set-payload>
            </ee:message>
        </ee:transform>
        <logger doc:name="Logger" doc:id="bhcobk" message="#[payload]"/> (6)
  </flow>
```

<table><tbody><tr><td><i data-value="1"></i><b>1</b></td><td>HTTP Listener connection configuration example</td></tr><tr><td><i data-value="2"></i><b>2</b></td><td>Autodiscovery configuration that references the flow <code>currentTimeFlow</code><div><p>If you try this app, replace <code>YOUR_API_ID_HERE</code> with the <code>apiID</code> value of an Mule Gateway API instance associated with the <a href="#oas-spec">OAS spec example</a>.</p></div></td></tr><tr><td><i data-value="3"></i><b>3</b></td><td>Flow <code>currentTimeFlow</code></td></tr><tr><td><i data-value="4"></i><b>4</b></td><td>HTTP listener with endpoint configuration <code>path="/current-time"</code><div><p>This endpoint matches the name of the endpoint defined in the <a href="#oas-spec">OAS spec example</a>.</p></div></td></tr><tr><td><i data-value="5"></i><b>5</b></td><td>Transform Message component that uses DataWeave to get the current time in the specified format<div><p>The format matches the formats defined in the <a href="#oas-spec">OAS spec example</a>.</p></div></td></tr><tr><td><i data-value="6"></i><b>6</b></td><td>Logger component that prints the current time (in the <code>payload</code>) upon requests to <code><a href="http://localhost:8081/current-time">http://localhost:8081/current-time</a></code> when the app is running</td></tr></tbody></table>
