AWS Marketplace

API monetization with Stripe, Amazon API Gateway, and Moesif

API monetization is a sought-after capability for organizations looking to create new streams of revenue with existing API and service infrastructure. With Amazon API Gateway and Moesif API Monetization, organizations can implement a well-architected API monetization strategy.

Moesif, an API analytics and monetization platform, is a member of the AWS Partner Network (APN) and is available in AWS Marketplace. Moesif helps organizations to understand their customers API usage, improve product-led growth, and monetize their APIs through metered billing.

This post demonstrates how to generate revenue from your APIs by metering API usage through Moesif and Amazon API Gateway, and use to invoice and collect payment for the API usage.

Solution overview

The integration works by forwarding API logs from your Amazon API Gateway instance to Moesif via an Amazon Kinesis Data Firehose. Then, you create a billing meter in Moesif, which defines what you want to meter, such as API transactions, and links it to a plan and price. You can connect Moesif to your billing system such as Stripe, SAP, or Chargebee for automatic invoicing and payments. You can deploy this solution without downtime with the available AWS CloudFormation template.

Prerequisites

For this setup, it is assumed that you have:

For this guide, the API key ID is used as the Moesif UserId. However, you can define any Amazon Gateway context variable as the user ID. See enabling user tracking for Moesif and Amazon API Gateway.

Solution walkthrough: End-to-end monetization with Stripe, Amazon API Gateway, and Moesif

A. Create your product and price in Stripe

To create a product and price in Stripe, do the following:

  1. Sign in to Stripe and proceed to the Products In the top right corner, click Add product
  2. Add the following details:
  • Name: Stripe Test
  • Description: Test Product
  • Pricing model: Standard pricing
  • Price: $0.10
  • Billing period: Monthly
  • Usage is metered: Yes (check the box)
  • Charge for metered usage by: Sum of usage values during period
  • Price description: My Price
  1. Click Save product

B. Enable the Moesif-Stripe integration

To connect Stripe and Moesif, follow these instructions on the Moesif page Integrating with Stripe.

C. Create a billing meter

To see the specific steps required to set up a billing meter, refer to the directions at Creating Billing Meters.

For this solution, you will create a billing meter using the Event Count metric. This metric counts every request and sends it to the billing provider. To configure the billing meter, do the following:

  1. In Moesif, on the left navigation, select Billing Meters.
  2. On the top right, select Add Billing Meter.
  3. On the New Billing Meter screen, in the Link to fields, populate the following values:
    1. Billing provider: Stripe
    2. Product: My API
    3. Price: API call
  4. In the Filters/Events Where, select the following values:
    1. request.URI route is /test-service/ or the endpoint of your choice you want to monetize
    2. response.Status.Code = 200 OK
  5. Under Metrics select Volume > Event count as the metric to bill upon.
  6. To create and activate the Billing Meter, in the upper right, select Create.
  7. In the dialog that appears, click Create Billing Meter.

The next modal to appear enables users to perform an optional meter test to ensure the meter is working correctly. For more information on Meter Tests, see the Moesif documentation.

D. Clone and Deploy The Monetization Code

Next, pull down the monetization code from github and deploy the /register endpoint.

Here’s what the endpoint will do:

  • Use the API Gateway JS client to create an API key and add it to a usage plan.
  • Create a user in Stripe and subscribe them to the API subscription.
  • Create the CompanyID and UserID in Moesif.
  • Create an API key.

If you already have User and Company identifiers in Moesif, you can set a Custom Id Mapping.

1. Clone the github repo

  1. In a console or Terminal window on your local system, clone the Github repo to your local machine.
  2. Once the cloning is complete, open the moesif-aws-gateway-stripe-api-monetization-demo directory in your favorite development tool or text editor.

2. Install the project dependencies

To install the dependencies in the node_modules folder in the root of the project, run npm install in the Terminal.

3. Create the .env file

In the root directory of the project, open the .env file. Populate the key-value pairs with the necessary info. The exact location for each of these values can be found in the Deployment section of the github project’s README. Once populated, save the .env file.

4. Create the app.js file

In the root directory of the repository you will find an app.js file, which contains the application’s logic. The code imports a few dependencies and configures the Stripe SDK, Amazon API Gateway client, and the Moesif middleware. It also creates the /register endpoint to run on port 5000. Let’s briefly go over the code inside the /register endpoint inside the app.js file.

The first bit of code creates the API key and associates that API key with a Usage Plan.

Code to generate an API key

Next, there is code to create a customer in Stripe. To do this, use the stripe.customers.create function.

code to create a customer in Stripe

Under the stripe.customers.create function, use the stripe.subscriptions.create function to create a subscription for the customer in Stripe.

Code to create a subscription in Stripe

Next, you will call the Moesif middleware’s updateCompany function. This maps the Stripe subscription.id to the companyId in Moesif.

Code to map the Stripe subscription to the companyId in Moesif

Below is the code that calls the updateUser function to update the Moesif user profile with the AWS API key ID as the userId and the Stripe subscription.id as the companyId.

update the Moesif user profile with the AWS API key

Lastly, the following code returns a 200 OK response and the API key in the response body.

code returns a 200 OK response and the API key

With the code ready, you will need to run the endpoint. To run the endpoint locally, open a Terminal window pointing to the root directory of your project. In the terminal, run node app.js.

5. Send a test request to the /register endpoint

  1. In Postman (or comparable tool), create your request with the following information:
    Request Type: POST
    Endpoint URL: http://localhost:5000/register
    Request body:

    {
        "firstname": "Userfirstname",
        "lastname": "Userlastname",
        "email": "test@test.com"
    }

    The response should contain an API key for the user.

  2. In Stripe, navigate to the Customers page. You should see your new user in the list.
    Select the newly added customer in the list. On the next screen, you should see that the customer is also subscribed to your APIs subscription.

E. Call your API using the generated API key

Your next step is to call the API using the generated key from the /register endpoint.

1. Use Postman to send the request

Use the endpoint you set up the billing meter for in Step C to send a request. In Postman, do the following:

  • Put the invoke URL for your API Gateway API endpoint as the request URL.
  • On the Authorization tab select the Type as API Key.
  • Populate the API key details:
    • Set the Key as x-api-key
    • Set the Value as the generated API key from the Postman call in step D.5.
    • Set the Add to field value to Header.
  • In the upper right, select Send.

2. Confirm that Moesif received the request

In Moesif, navigate to the Live Event Log screen. You should see the request you just sent with the User ID and Company ID populated with the AWS API key and Stripe subscription ID.

The following screenshot shows a Moesif Live Event Log screen. On the left is the navigation sidebar. In the center pane, the filters are set to request. URI route is /prod (5). Five event entries are displayed, along with User IDs, Company IDs, and dates and times.

Screenshot of the Moesif Live Event Log screen

F. Check Stripe for usage

  1. In Stripe choose the Customers tab and select the customer you made the API call with in step E. Choose the My API subscription entry. On the Subscriptions screen, it should show the My API subscription as active and billing monthly.
  2. On the same Subscription screen, scroll down and select View Usage. The modal that appears shows the usage for the API that has been reported to Stripe from Moesif.

Conclusion

This blog post demonstrated how to monetize your APIs with Amazon API Gateway, Stripe, and Moesif. This solution automatically meters API usage and sends it to Stripe for users to be invoiced and to process payments. Using AWS and Moesif together gives you ongoing visibility into your APIs and the ability to create new revenue streams using your APIs. A working example developer portal using Amazon API Gateway, Moesif, and Stripe is available on GitHub.

About Authors

Headshot of Matt Tanner, Head of Developer Relations at Moesif

Matt Tanner

Matt Tanner is the Head of Developer Relations at Moesif, an API observability and monetization platform. Matt works with developers to improve the Moesif platform and ensure their success through writing technical guides, docs, and working with customers who are in various stages of adopting Moesif. He enjoys cycling, rock climbing, and building new products.

Headshot of Volker Mueller, AWS Senior Solutions Architect

Volker Mueller

Volker Mueller is an AWS Senior Solutions Architect supporting Startup ISV customers in the US, with more than 25 years of international experience. He lives with his family in Austin, TX, where he enjoys to swim, cycle and run.