Creating your first Stripe Charge with Python + Flask in 5 minutes
In this short series, we are going to look at how to create a charge to Stripe in a number of their officially supported languages!
Today, we are going to look at how to do so with Python and Flask.
The expectations are that you have both Python 3 and
pip3 installed and have your Stripe API keys setup and ready to go.
Setting up .env
Fetch your keys from Stripe and replace the following in the file:
Set up the file to look like the following:
- Fetches and sets OS env from .env file.
- Sets the Stripe API key.
- Sets a route
/api/chargethat only takes the
POSTmethod and creates a charge based on the amount we pass.
Running the server
python3 server.py will start the server on port 5000.
http POST http://localhost:5000/api/charge amount:=1700 receiptEmailemail@example.com (using HTTPie) will come back with success. Check your Stripe dashboard and you will see a charge made for AUD\$17.00! Hooray!
I chose to use HTTPie because I feel it is a fun tool that more should know about! Alternative, you could do the above using
curl as well (or anything that can make a POST request for a matter of fact).
Resources and Further Reading
Image credit: Pankaj Patel
1,200+ PEOPLE ALREADY JOINED ❤️️
Get fresh posts + news direct to your inbox.
No spam. We only send you relevant content.