1
Create an API Key in the partner dashboard
If you’re just getting started, be sure to create an API Key in test mode to safely distinguish between testing and production.
New API Key
button and give your API Key a name. With this key we can now interact with
the Flex API.2
Define a product to sell
Always keep sensitive information about your product inventory, such as price and availability,
on your server to prevent customer manipulation from the client. Define product information when
you create the Checkout Session using predefined price IDs or on the fly with price_data.For this example we’ve created a product that is eligible via a letter of medical necessity. To learn more
about creating products and their eligibility review the Product docs or Product API.The response we get back is:
3
Define the pricing model
Given the The response we get back is:
product_id
that was returned to in the previous payload we’ll define a pricing model where we’ll charge
$49.99 every month4
Supply success and cancel URLs
Specify URLs for success and cancel pages—make sure they’re publicly accessible so Flex can redirect customers to them.
You can also handle both the success and canceled states with the same URL.
success_url: https://withflex.com/thank-you?success=true
cancel_url: https://withflex.com/thank-you?canceled=true
5
Build your checkout
On your site you should have an order preview page. On the order preview page you will add a button that will
trigger your server to generate a checkout session.
6
Generate a checkout session
On the server you will generate a checkout session.
7
Redirect users to checkout
The response we got back looks like:
8
Provision and monitor subscriptions
After the subscription signup succeeds, the customer returns to the
success_url
specified, which initiates a checkout.session.completed
event.
When you receive a checkout.session.completed
event, you can provision the subscription. Continue to provision for each billinging cycle as you receive
invoice.paid
events. If you receive an invoice.payment_failed
event, notify your customer and perform the appropriate action your team
has decided at this moment.To determine the next step for your system’s logic, check the event type and parse the payload of each event, such as invoice.paid.
Store the subscription_id and customer_id event objects in your database for verification.9
Try it out
Navigate to the redirect_url and use any of these tests cards to simulate a payment.Payment succeeds:
4242424242424242
Payment is decline: 4000000000009995
If you have setup your webhooks, you should see the checkout.session.completed
upon a successful payment.10
Congratulations!
You have a basic subscriptions integration working!