# Twitch webhooks with python

**URL:** <https://staging-discuss.dev.twitch.com/t/twitch-webhooks-with-python/19728>\
**Category:** API\
**Created:** [January 27, 2019, 5:45pm UTC](https://staging-discuss.dev.twitch.com/t/twitch-webhooks-with-python/19728 "2019-01-27T17:45:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rqd3](https://avatars.discourse-cdn.com/v4/letter/r/3ec8ea/32.png) [@rqd3](https://staging-discuss.dev.twitch.com/u/rqd3)\
**Post date:** [January 27, 2019, 5:45pm UTC](https://staging-discuss.dev.twitch.com/t/twitch-webhooks-with-python/19728/1 "2019-01-27T17:45:52Z")

</div>

I’m trying to set up a twitch follow alert integration with my discord server. Here’s my python code so far

payload ={‘hub.mode’:‘subscribe’, ‘hub.topic’:'[https://api.twitch.tv/helix/users/follows?first=1&to\_id=’+apireqs.user\_id](https://api.twitch.tv/helix/users/follows?first=1&to_id='+apireqs.user_id),  
 ‘hub.callback’:callback\_url,  
 ‘hub.lease\_seconds’:“86400”, ‘hub.secret’:‘asecretpass’}  
url = ‘[https://api.twitch.tv/helix/webhooks/hub/](https://api.twitch.tv/helix/webhooks/hub/)’  
r = requests.post(url,headers=header, data= payload)

g = requests.get(url=callback\_url,params=f’{callback\_url}   
hub.mode=subscribe&   
hub.topic=https://api.twitch.tv/helix/users/follows?first=1&to\_id={apireqs.user\_id}&   
hub.lease\_seconds=864000&   
hub.challenge=HzSGH\_h04Cgl6VbDJm7IyXSNSlrhaLvBi9eft3bw’)

However, I get no messages on my server? What am I doing wrong? my POST request gives a 202 status code and my GET request gives a 200 one.

---

<div class="post-metadata">

**Author:** ![Dist](https://d1d6mrmdu7ztse.cloudfront.net/user_avatar/staging-discuss.dev.twitch.com/dist/32/7192_2.png) [@Dist](https://staging-discuss.dev.twitch.com/u/Dist)\
**Post date:** [January 27, 2019, 6:55pm UTC](https://staging-discuss.dev.twitch.com/t/twitch-webhooks-with-python/19728/2 "2019-01-27T18:55:33Z")

</div>

Is your GET handler responding with the `hub.challenge` as well as the 200 status code?

---

<div class="post-metadata">

**Author:** ![rqd3](https://avatars.discourse-cdn.com/v4/letter/r/3ec8ea/32.png) [@rqd3](https://staging-discuss.dev.twitch.com/u/rqd3)\
**Post date:** [January 27, 2019, 7:32pm UTC](https://staging-discuss.dev.twitch.com/t/twitch-webhooks-with-python/19728/3 "2019-01-27T19:32:05Z")

</div>

I’m not sure I’m following you. Sorry I’m new to webhooks.

My get request (which as far as I know is a verification request) is giving me a 200 response but I think the code is plain wrong. I really need help figuring out how to properly verify the response.

---

<div class="post-metadata">

**Author:** ![Dist](https://d1d6mrmdu7ztse.cloudfront.net/user_avatar/staging-discuss.dev.twitch.com/dist/32/7192_2.png) [@Dist](https://staging-discuss.dev.twitch.com/u/Dist)\
**Post date:** [January 27, 2019, 7:51pm UTC](https://staging-discuss.dev.twitch.com/t/twitch-webhooks-with-python/19728/4 "2019-01-27T19:51:33Z")

</div>

I don’t know python so can’t help directly with the code.

The process though, is that you send a POST to Twitch with your subscription request.  
Twitch then send a GET request to your callback URL to make sure it’s reachable which includes a `hub.challenge`, you have to respond to this GET request by replying the `hub.challenge` back to Twitch along with a 200 status code.

At this point, the subscription should be successfully created and you can verify that by checking the Get Webhook Subscriptions endpoint: [https://dev.twitch.tv/docs/api/reference/#get-webhook-subscriptions](https://dev.twitch.tv/docs/api/reference/#get-webhook-subscriptions)

---

<div class="post-metadata">

**Author:** ![Dist](https://d1d6mrmdu7ztse.cloudfront.net/user_avatar/staging-discuss.dev.twitch.com/dist/32/7192_2.png) [@Dist](https://staging-discuss.dev.twitch.com/u/Dist)\
**Post date:** [January 27, 2019, 10:22pm UTC](https://staging-discuss.dev.twitch.com/t/twitch-webhooks-with-python/19728/6 "2019-01-27T22:22:49Z")

</div>

No there aren’t webhooks for cheers or subscriptions, at least not yet.

And please don’t add off-topic questions to threads when you already had created a thread on this subject and should have asked there.

---

<div class="post-metadata">

**Author:** ![AFO\_UYI](https://avatars.discourse-cdn.com/v4/letter/a/5f9b8f/32.png) [@AFO\_UYI](https://staging-discuss.dev.twitch.com/u/AFO_UYI)\
**Post date:** [February 1, 2019, 6:57pm UTC](https://staging-discuss.dev.twitch.com/t/twitch-webhooks-with-python/19728/7 "2019-02-01T18:57:37Z")

</div>

I never used twitch’s webhooks, but I know about Python. I will tell you some errors or weird parts of your code, and with that, Dist could give you some notes for that things you dont understand about the documentation of twitch’s webhooks, if you have more doubts or problems.

Firts, the most weird part to me:

```
g = requests.get(url=callback_url,params=f’{callback_url} 
hub.mode=subscribe&
hub.topic=https://api.twitch.tv/helix/users/follows?first=1&to_id={apireqs.user_id}
&hub.lease_seconds=864000&hub.challenge=HzSGH_h04Cgl6VbDJm7IyXSNSlrhaLvBi9eft3bw’)

```

In your code, doesnt appear `callback_url`'s value, but according to the variable name (and the use as value for payload’s key hub.callback), is a url of your own server. So, if you put that on `url` param of `requests.get()`, the request was made to yourself, not to twitch.

Furthermore, the `params` param (:V) of `requests.get()` is bad formated. Assuming that `callback_url = "kappa.tv"`, `params`'s value start with `params="kappa.tvhub.mode=subscribe&...etc"`. And `requests.get()` then make a request over `kappa.tv?kappa.tvhub.mode=subscribe&...etc` (you can see the resulting url with `print(g.url)` ). Also is bad formated because you have hub.topic url as a GET param (those appears in the request url) being error prone on parse the URL for target server. And this is the second point.

payload dictionary is well formated. But is used on the wrong params. `params` param put the key-values appearing in the URL (for GET request), `data` put the key-values on the request body (for POST request) BUT … the content-type value for `data` is `application/x-www-form-urlencoded`, where twitch’s docs is asking for a JSON body, being the content-type `application/json`. For that purpose you just need to use `json` param instead of `data`. So your request code would be `r = requests.post(url,headers=header, json=payload)` (I dont know how you are using `headers`. if you are using it for the content-type specification, just avoid that and use the `json` param).

Finally, a third point about the GET request you are doing. According the docs (the link posted by dist before) , that request just have `after` and `first` params. Are not the same as the POST request.

Good luck xD

---

<div class="post-metadata">

**Author:** ![system](https://d2spem0quai2ol.cloudfront.net/original/2X/0/05909729875cce3b53e59d5561c4d23bb14948fc.png) [@system](https://staging-discuss.dev.twitch.com/u/system)\
**Post date:** [March 3, 2019, 6:57pm UTC](https://staging-discuss.dev.twitch.com/t/twitch-webhooks-with-python/19728/8 "2019-03-03T18:57:39Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
