Asked 1 month ago by StellarWatcher787
Why is my Django Stripe webhook endpoint returning a 404 error?
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
Asked 1 month ago by StellarWatcher787
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
I'm trying to set up a Stripe webhook in my Django project (first timer), but I keep getting a 404 error when Stripe sends an event.
I suspect the URL configuration might be off. Below are my configurations and what I've tried:
Project-level urls.py
PYTHONurlpatterns = [ path('my_app/', include('my_app.urls')), ... ]
App-level urls.py
PYTHONurlpatterns = [ ... path('stripe_webhook/', views.stripe_webhook, name='stripe_webhook') ]
views.py (in my_app)
PYTHON@csrf_exempt def stripe_webhook(request): print("enter webhook") stripe.api_key = settings.STRIPE_SECRET_KEY_TEST payload = request.body signature_header = request.META.get('HTTP_STRIPE_SIGNATURE') webhook_secret = settings.STRIPE_WEBHOOK_SECRET_TEST ...
In Stripe’s local listener, I registered the endpoint as:
PLAINTEXTlocalhost:8000/stripe_webhook/
When I trigger an event with stripe trigger customer.created
, I get a success message from Stripe but then see this log:
BASH> Ready! You are using Stripe API Version [2024-09-30.acacia]. Your webhook signing secret is XXXXX (^C to quit) 2024-12-05 22:27:54 --> customer.created [xxx] 2024-12-05 22:27:54 <-- [404] POST http://localhost:8000/stripe_webhook/ [xx]
My server logs also show: Not Found: /stripe_webhook/
whether in production or locally.
This leads me to think the path configuration doesn't match between Django and Stripe. I have double-checked that my keys (STRIPE_PUBLIC_KEY_TEST
, STRIPE_SECRET_KEY_TEST
, and STRIPE_WEBHOOK_SECRET_TEST
) in my .env file match the ones on Stripe's platform.
Any insights on what might be causing this issue? Note that it might be a URL mismatch: based on my project-level urls.py, the actual endpoint could be at /my_app/stripe_webhook/
instead.
Thanks for any help!
Comments 0
•Answers 2
•Views 53
Answer by TychoAI Agent • 1 month ago
Your app’s URL configuration implies that the webhook endpoint is actually at:
localhost:8000/my_app/stripe_webhook/
yet your Stripe CLI listener is forwarding to:
localhost:8000/stripe_webhook/
Since your project-level urls.py includes your app’s urls under the 'my_app/'
path prefix, Django expects the endpoint to be prefixed with /my_app/
.
You have two options:
Update the forward-to URL in Stripe CLI:
Change the forward URL so that it matches your actual endpoint:
BASHstripe listen --forward-to localhost:8000/my_app/stripe_webhook/
Reconfigure your URL patterns:
If you prefer not to include the 'my_app/'
prefix, update your project-level urls.py to include the app’s urls without a prefix:
PYTHONurlpatterns = [ path('', include('my_app.urls')), # ... ]
This way, your endpoint will be directly accessible at /stripe_webhook/
.
Either option should resolve the 404 issue.
Tycho is an AI agent, that grounds responses in various sources like documentation, code bases, live discussions, and relevant posts. Want to chat privately with Tycho?
No comments yet.
Answer by GalacticPioneer460 • 3 months ago
It looks to me like you are prefacing your app URLs with my_app/
so I would expect that to be in the final URL. I have a long-standing (3+ years) Django integration with Stripe and, when I check my root urls.py
file I see that I have my main app URLs specified like so
PYTHONpath('', include('payments.urls')),
This avoids needing to include an app prefix so localhost:8000/webhooks/
works for me.
Since you specify my_app/
, you should try localhost:8000/my_app/stripe_webhook
. You can use the CLI to trigger webhook events so you can check if that works.
Another approach I like to use is to add an API method check in my webhook function and respond to a GET request with something like <h1>Hello Webhook!</h1>
. Something like this:
PYTHON@csrf_exempt def stripe_webhook(request): if request.method == "GET": # Respond with webpage indicating success else: # Do webhook-y stuff here
That way you can easily test if the route is working by putting the URL in your web browser.
No comments yet.
No comments yet.