WSO2 is now Choreo LabsLearn more

Trusted AI Governance
Build

Create agents and AI apps with the tools and languages your teams already use.

Connect

Link agents securely to your APIs, data, models and business systems.

Run

Deploy, observe and operate agents and AI apps reliably at any scale.

Govern

Control identity, access, cost and policy for every agent in production.

Agent Management

Developer Platform

  • API Management Cloud

    Design, secure and publish APIs with governance and analytics built in.

    Sign Up Now
  • Integration Cloud

    Connect apps, data and AI agents with low-code and pro-code integration.

    Sign Up Now
  • Identity & Access Management Cloud

    Secure sign-in and access for customers, partners, workforce and agents.

    Sign Up Now
  • Agent Management Cloud

    Build, run and govern AI agents with full visibility and control.

    Sign Up Now
  • Developer Platform Cloud

    Ship cloud-native apps faster on an internal developer platform.

    Sign Up Now

Self-Hosted

Full control over your stack. Deploy directly to your own servers, bare metal, cloud, or Kubernetes environment. Your data never leaves your perimeter.

Product Downloads
Choreo Labs
AI Governance
Platform
Solutions
Resources
Blog

Programmatically download API usage statistics

WSO2 API Cloud has a rich set of reports built in. In addition to that, we have now added the ability to programmatically pull API invocation data via a REST API so you can do your own reporting and integrate it with your other systems.

For this example, I will pull down statistics on the API calls that I made on a particular day. Here's how:

1. Follow our earlier blog post to register your client and obtain your OAuth token (steps 1 and 2 in that blog post).

Important: on step 2.3 (Request the token), make sure to use apim:subscribe as the scope:

$ curl -k -d "grant_type=password&username=email@org_key&password=pwd&scope=apim:subscribe" -H "Authorization: Basic Base64EncodedclientIdclientSecret" https://gateway.api.cloud.wso2.com/token

{
  "access_token":"fac22c3b-efc1-3c16-92e8-56ab96a0f2f6",
  "refresh_token":"8711733a-a526-3479-bd43-a2f8a2a9c638",
  "scope":"apim:subscribe",
  "token_type":"Bearer",
  "expires_in":3600
}

2. Invoke the statistics API:

2.1 Create a json file with the parameters - the timeframe and the statistics ID:

  • getTopAppUsers - Top Users For Applications
  • getAppApiCallType - API Usage from Resource Path
  • getPerAppAPIFaultCount - Faulty Invocations per Application
  • getProviderAPIUsage - API Usage per Application

For example, here's the stat_payload.json that I used:

{
    "statisticsType" : "getProviderAPIUsage",
    "toDate":"2018-02-02 00:00",
    "fromDate": "2018-02-01 00:00"
}

2.2 Invoke the API to get the stats:

$ curl -X POST -H "Authorization: Bearer fac22c3b-efc1-3c16-92e8-56ab96a0f2f6" -H "Content-Type: application/json" -d @stat_payload.json https://gateway.api.cloud.wso2.com/api/am/user/subscriber/statistics

{
  "success":true,
  "message":"Successfully retrieved the statistics data for the statistics type getProviderAPIUsage for the user [email protected]@wso2dmitry",
  "data":"[
     {
       \"appName\":\"Default Application\",
       \"apiCountArray\":[
          {
            \"apiName\":\"Heroes ([email protected]@wso2dmitry)\",
            \"count\":719
          },
          {
            \"apiName\":\"Phones ([email protected]@wso2dmitry)\",
            \"count\":1063
          },
          {
            \"apiName\":\"WorldBank ([email protected]@wso2dmitry)\",
            \"count\":14
          }
       ]
     }
   ]"
}

This is just one of the APIs that WSO2 Cloud exposes. To see all other APIs, check out our documentation.