cloudblog
2018/03/20
March 20, 2018
3 min read

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.