stripe-node
Cloudflare Worker TemplateThis is a template for setting up a Cloudflare Worker which uses stripe-node
. The wrangler
CLI tool is used for publishing.
To generate using wrangler
wrangler generate projectname https://github.com/stripe-samples/stripe-node-cloudflare-worker-template
cd projectname
npm install
Further documentation for Wrangler can be found here.
To add your STRIPE_API_KEY as a plaintext environment variable via wrangler:
Rename and move the .dev.vars.example
file into a file named .dev.vars
. For example:
cp .dev.vars.example .dev.vars
Example .env file:
STRIPE_API_KEY='sk_test_xxx'
You will need a Stripe account in order to run the demo. Once you set up your account, go to the Stripe developer dashboard to find your API keys.
Finally, you can run this example by the following command:
npm run dev
You will get the local application URL like the following:
[mf:inf] Ready on http://0.0.0.0:51219
[mf:inf] - http://127.0.0.1:51219
[mf:inf] - http://192.168.86.21:51219
[mf:inf] - http://172.18.96.89:51219
You can use the Stripe CLI to easily spin up a local webhook.
First install the CLI and link your Stripe account.
stripe listen --forward-to http://{REPLACE_TO_YOUR_LOCAL_APPLICATION_URL}/webhook
The CLI will print a webhook secret key to the console. Set STRIPE_WEBHOOK_SECRET to this value in your .env file.
You should see events logged in the console where the CLI is running.
When you are ready to create a live webhook endpoint, follow our guide in the docs on configuring a webhook endpoint in the dashboard.
To add your STRIPE_API_KEY as a plaintext environment variable via wrangler:
[vars]
STRIPE_API_KEY = "<YOUR API KEY HERE>"
Now, you can build and publish your worker using:
wrangler publish
This will build your application and publish it. If this is your first time publishing, it will create a new worker named my-stripe-worker
under your account. If all goes well, the command will output a URL that looks something like https://my-stripe-worker.<your user name>.workers.dev
where your worker is available.
Cloudflare Workers offer a secrets infrastructure for storing an encrypted secret. You can use this instead of relying on an environment variable.
wrangler secret put STRIPE_API_KEY
. This will prompt you to enter your secret (ie. your Stripe API key). This only needs to be done the first time you are configuring up your new worker. It will persist across deployments. Once set up, you can publish using:
wrangler publish
Depending on the settings of your local machine, there may be failures when attempting to start the service in a local environment using Wrangler. According to a GitHub issue, it could be related to the certificate file or network settings.
To avoid using an invalid certificate file, please try starting the application with the following command:
NODE_EXTRA_CA_CERTS="" npm run dev
If it still does not work, you may need to change the /etc/hosts/
file and add the following settings:
127.0.0.1 localhost