Enabling Monetization of APIs
API Monetization is being able to charge developers for APIs that contain business information needed to build applications. This is done using billing plans. WSO2 API Manager allows you to manage, govern and monetize APIs.
A typical use case is where an API creator implements an API and the API publisher publishes the APIs to a gateway by specifying throttling tiers and related security and governance configurations. Allowed users can list the APIs available in the gateway using the API Store. These subscribers can subscribe to these available APIs and invoke them. In this billing model, API subscribers get billed based on the subscribed tiers. In WSO2 API Manager, the throttling tiers act as a billing plan when invoked. In order to define a new billing plan, users need to define a new throttling tier plan in WSO2 API Manager to reflect those business rules.
Subscription workflow
To activate the subscription using throttling tiers, use the WSO2 API Manager Subscription workflow. In the default workflow, subscriptions are automatically active. However, when integrating with the billing engine, subscriptions are active 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 are 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, the user is redirected back to WSO2 API Manager and the subscription is activated.
To configure the default subscription workflow, follow the instructions given in Implementing Workflow Extensions.
Enabling and disabling billing related developer subscriptions
A developer can subscribe to an API, invoke it and get invoiced for it but avoid the bill payment. To prevent this, 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.
To enable/disable subscriptions through the UI,
Sign in to the API Publisher.
https://<hostname>:9443/publisher(e.g. :https://localhost:9443/publisher).In the HOME menu, click MANAGE SUBSCRIPTIONS.
To disable the subscription of a particular API, click Block. Now, an API Store user is unable to invoke that API until it is re-enabled.
In the Manage Subscriptions tab, subscriptions of the APIs are only visible to the creator of that API. For example, if User1 created 2 APIs and User2 created 1 API, then User1 can only view the subscriptions for the APIs that were created by User1.
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
For the billing engine to be able to retrieve data, you have to configure WSO2 API Manager Analytics. The API invocation related events are published to WSO2 APIM Analytics, which then persists all the events and makes them available in internal tables. To help the billing engine work efficiently, you may also want summarized data sets based on the raw event data.
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.workflow
For more information about the event details, see Introducing the WSO2 API Manager Statistics Model.
The sections below describe how to monetize your API.
Implement workflow extensions
To implement the default subscription workflow, start a new maven project and create a new class named SubscriptionBillingWorkflow. You need to implement the methods as per the source code here. If you need to modify the existing workflow (or even write a new workflow, you can create a new project and a new class. This class name must be the name you use in step 4. You could do so within the implemented methods in the above code. Once you have implemented the workflow, proceed to deploying the workflow extention, which is explained in the section below.
Deploying the extended workflow
Build the maven project (subs-billing-workflow in the source). Optionally, you can download the built jar file from here.
Copy and paste the built jar into the
<API-M home>/repository/component/libfolder (If you already started the server, restart it before proceeding further).Log in to the WSO2 API Manager web console (
https://localhost:9443/carbon) and browse the resources.Open the
/_system/governance/apimgt/applicationdata/workflow-extensions.xmlfile. Replace theSubscriptionCreationtag with the value given in the example below,<SubscriptionCreation executor="org.wso2.sample.apimgt.workflow.SubscriptionBillingWorkflow"/>
Configuring the billing engine
This is a sample billing engine provided for you to test out this feature.
Create a MySQL database called
billing. This is the database that will store information regarding the application users, the billing plans, and the invoices.Download the sample billing engine provided for WSO2 API Manager. You can also build it from here.
To deploy the
.warfile in a Tomcat container, start a Tomcat server and deploy the downloaded.warfile in itswebappsfolder.Locate the deploy webapp and edit the
<apim-billing-engine-home>/WEB-INF/classes/datasource.propertiesfile as shown below, in order to enable the webapp to write data into the database and pull data from the Analytics server.url=jdbc:mysql://localhost:3306/billingusername=rootpassword=passdriverClassName=com.mysql.jdbc.Driverdialect=org.hibernate.dialect.MySQL5InnoDBDialectapimStoreUrl=https://localhost:9443/apimUserName=adminapimPassword=admindasUrl=https://localhost:9444/dasUserName=admindasPassword=adminjksPath=<extracted_apim-billing-engine-home>/WEB-INF/classes/wso2-jks/wso2carbon.jks
Navigate to the main page of the billing engine at http://localhost:8080/apim-billing-engine-1.3.0/app/main.
Sign up as a user.
Log in using the newly created user. This is when the tables are created in the billing database that you created in step 1.
You can define 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: Charges a fixed fee per request.