This project was created to show a simple but end-to-end solution for monetizing APIs with Tyk, Stripe, and Moesif.
For the full end-to-end configuration of this project, check out our complete guide!
To run this project you must have Node and npm installed.
You must also have:
- an active instance of Tyk API gateway up and running with at least one API proxied through it
- Your endpoints in Tyk should be protected by API Key
- an active Stripe account with at least one Product and Price created
- Have an active Stripe integration and billing meter in Moesif which includes the endpoints you want to monetize. To set one up, check out our guide on it here.
After you cloned your repo, in the root folder of the repo, to set up your environment and install dependencies, run npm install
.
In the .env
file, you will need to plug in a few details in order to make this work.
STRIPE_KEY will contain the key to access the Stripe APIs.
STRIPE_PRICE_KEY will contain the key to your Product's Price in Stripe
MOESIF_APPLICATION_ID will contain your Moesif Application ID.
TYK_AUTH_KEY This can be found in Tyk by navigating to the Users page and clicking on a user's entry (likely your own).Once you’ve clicked on the user, you will be able to see the user's Tyk Dashboard API Access Credentials. This is the key you’ll plug into the config.
TYK_API_ID The API ID for the endpoint can be found by going to the APIs screen in Tyk and copying the ID for your endpoint.
TYK_API_NAME The API Name for the endpoint. This can be found in the same place as the API ID.
TYK_URL This will be the URL of the Tyk Dashboard API. Usually, this will be hosted on port 3000 but it may be different if you have a custom configuration.
To run the project, run node app.js
. This will bring up the backend and frontend for the project.
The backend endpoint can be reached at http://localhost:5000/register. The frontend can be accessed through http://localhost:5000.
In the frontend, you can add in an email, first name, and last name into the fields on the screen. Then click Register to generate an API key.
IF you want to use the backend directly, you will need to create a request to the http://localhost:5000/register and include a JSON request body. It will look like this:
{
email: "[email protected]",
firstname: "firstname",
lastname: "lastname",
}
Once sent, the response brought back from the /register endpoint will contain an API key.
The API key can then be used to send requests to your API key protected APIs in Tyk.
Once you have registered a user using the /register endpoint, you should see:
- A new API key created in Tyk under Consumers which has the Stripe Customer_ID in the Alias field.
- A new Customer in Stripe with the same Customer_ID you see in the Alias field in your Tyk API key
Once you have sent a request to a protected API in Tyk, you should see the request in Moesif. The request should contain:
- The route information
- Stripe metadata (you'll need to inspect the request to see this)
- The User ID in Moesif will be the Stripe Customer_ID
- The Company ID in Moesif will be the Stripe Subscription_ID
Usage will be available in Stripe in a few hours. If data still isn't in Stripe after a few hours, reach out to Moesif support for help with your configuration.
For all changes and contributions we use the Github Pull request process as our workflow. Please see detailed instructions in notion on how to create branch and create pull requests.
https://www.notion.so/moesif/Github-Flow-583da5aaa9b54d118bea1dd4db17b97f
Most articles are written in markdown. Quick guide here: https://www.markdownguide.org/cheat-sheet/