WSO2 API Manager allows you to manage, govern and monetize APIs. You can specify throttling tiers and list the APIs that are available in the Gateway using the API Store. Subscribers who subscribe to the listed APIs and invoke them are billed based on the throttling tiers. To define a billing plan you need to define a new throttling tier in WSO2 API Manager.
Subscription workflow
To activate the subscription using throttling tiers, you need to use the WSO2 API Manager Subscription workflow. In the default workflow, subscriptions are automatically active. But when integrating with the billing engine, subscriptions are active if and only if the billing engine accepts the user. This behavior can be achieved by extending the default subscription workflow. When a subscriber tries to subscribe to an API after the subscription workflow is configured, they will be redirected to the billing engine for authentication with the API manager workflow details. Then the billing engine accepts the user or signs them up as a new user to the system. After the user enrollment is done by the billing engine it will redirect them back to the WSO2 API manager and activate the subscription.
To configure the default subscription workflow, follow the instructions given in Implementing Workflow Extensions.
Configuring billing developer subscriptions
A developer can subscribe to an API, invoke it and get invoiced for it but avoid the bill payment. To stop this from happening there needs to be a mechanism that address this issue. With WSO2 API Manager subscriptions can be enabled and disabled at the publisher’s will. If the user doesn’t pay, the publisher can disable the subscription and re-enable it once the payment is made. This can be achieved in two ways.
To enable/disable subscriptions through the UI, login to the API Publisher and click the Manage Subscriptions tab
Controlling over exposure
Throttling tiers contain a Quota Reach Option, which might result in the gateway getting flooded with requests. You can use the Hard Level Throttling option to define the maximum number of requests per minute. For more information, see Setting Maximum Backend Throughput Limits
Configuring API Manager Analytics
You have to configure WSO2 API Manager Analytics in order to the billing engine to retrieve data. The API invocation related events are published to WSO2 APIM Analytics, which then persists all the events and makes them available in internal tables. You may also want summarized data sets based on the raw event data to help the billing engine work efficiently.
For details on how to configure WSO2 API Manager Analytics, see Configuring APIM Analytics.
Data retrieving model for monetization
Following are the events that are sent to WSO2 DAS:
org.wso2.apimgt.statistics.request
org.wso2.apimgt.statistics.response
org.wso2.apimgt.statistics.fault
org.wso2.apimgt.statistics.throttle
org_wso2_apimgt_statistics_destination
org.wso2.apimgt.statistics.workflow
For more information about the event details please refer Introducing the WSO2 API Manager Statistics Model.
Implement workflow extensions
To configure the default subscription workflow, start a new maven project and create a new class named SubscriptionBillingWorkflow. Download the source code from here.
Deploying the extended workflow
Build the maven project (
subs-billing-workflow
in the source).Copy and paste the built jar into the
<API-M home>/repository/component/lib
folder.Log in to the WSO2 API Manager web console and browse the resources.
Open the /_system/governance/apimgt/applicationdata/workflow-extensions.xml file. Replace the “SubscriptionCreation” tag with the value given in the example below
<SubscriptionCreation executor="org.wso2.sample.apimgt.workflow.SubscriptionBillingWorkflow"/>
Configuring the billing engine
Create a mysql database ‘billing’
Get the sample billing engine provide for WSO2 API Manager from here.
Build the project and locate the war file.
Deploy the war in the Tomcat container.
Locate the deploy webapp and edit the <apim-billing-engine-home>/WEB-INF/classes/datasource.properties file as shown below
url=jdbc:mysql://localhost:3306/billing
username=root
password=pass
driverClassName=com.mysql.jdbc.Driver
dialect=org.hibernate.dialect.MySQL5InnoDBDialect
apimStoreUrl=https://localhost:9443/
apimUserName=admin
apimPassword=admin
dasUrl=https://localhost:9444/
dasUserName=admin
dasPassword=admin
jksPath=<apim-billing-engine-home>/src/main/resources/wso2-jks/wso2carbon.jks
- Define the new billing plan
You can define the following two different types of billing plans based on the WSO2 API Manager billing model- Subscription-based Usage Plan : Charges based on the subscription fee and fee per additional request.
- Request-based Usage Plan : Charge a fixed fee per request
- Subscription-based Usage Plan : Charges based on the subscription fee and fee per additional request.
Install additional cApp
You can use this additional cApp to generate additional summary data used to feed the billing engine. The WSO2 DAS REST API was used to retrieve the data.
- Locate cApp from
apim-billing-engine/src/main/resources/DAS-cApp/dist/APIM_Billing_2.0.0.car
- Login to WSO2 APIM Analytics web console
- Go to carbon-application section and deploy the downloaded cApp
Configuring WSO2 API Manager
Define the data source for the billing engine. Since the workflow extension is used, define the billing engine user details data source configuration in the
<API-M home>/repository/conf/datasources/master-datasources.xml
file as given in the example below.<datasource> <name>BILLING_DB</name> <description>The datasource used for registry and user manager</description> <jndiConfig> <name>jdbc/BILLING_DB</name> </jndiConfig> <definition type="RDBMS"> <configuration> <url>jdbc:mysql://localhost:3306/billing?autoReconnect=true&</url> <username>root</username> <password>pass</password> <driverClassName>com.mysql.jdbc.Driver</driverClassName> <maxActive>50</maxActive> <maxWait>60000</maxWait> <testOnBorrow>true</testOnBorrow> <validationQuery>SELECT 1</validationQuery> <validationInterval>30000</validationInterval> <defaultAutoCommit>false</defaultAutoCommit> </configuration> </definition> </datasource>
To configure the billing engine URL open the
<API-M home>
/repository/conf/api-manager.xml
file and replace the value as given below<billingEngineUrl>http://localhost:8080/apim-billing-engine-1.0.0/app/main</billingEngineUrl>
- Since WSO2 API Manager is configured for monetization we can enable the API status as free or premium. To enable this flag edit the registry location
<API-M home>/_system/config/apimgt/applicationdata/tenant-conf.json
as shown below:To enable monetization set the “
EnableMonetization
” property totrue
.To define if the unlimited tier is paid, set the
IsUnlimitedTierPaid
property totrue
.
- Copy and paste the mysql jar to /repository/component/lib since the extension workflow uses the mysql connector.
- Enable Analytics by setting
<Analytics>
in api-manager.xml file
Deploy the extended workflow
Build the maven project (subs-billing-workflow in the source).
Copy and paste the built jar file into the
<API-M_HOME>/repository/component/lib
folder.Log in to the WSO2 API Manager web console and browse the resources.
Edit the
/_system/governance/apimgt/applicationdata/workflow-extensions.xml
file.Find the
SubscriptionCreation
tag and replace it as shown below<SubscriptionCreation executor="org.wso2.sample.apimgt.workflow.SubscriptionBillingWorkflow"/>
Test the system
- Create a subscription tier in the Admin Portal. For instructions on creating a tier, see Adding a new subscription-level throttling tier.
- Deploy the sample API in the API Publisher
- Edit the sample API. Go to the Manage tab and select the new tier you have created in the previous step. Click Publish
- Go to the API Store and subscribe to the sample API. You will be re-directed to the billing engine.
- Sign up using the billing engine. You will be re-directed to the Store with a successful subscription.
- Log in to the billing engine once more, using your credentials.
- Create a new billing plan according to your subscription tier as shown below.
- Invoke the sample API from the API Publisher.
Go to the billing engine and generate an invoice as shown below
You have to select the correct plan and the month in order to generate the invoice.
- Your invoice will be generated based on usage.