How to Get list of webhook's subscriptions


#1

Hi guys, I sent a registration to an webhook but I don’t know if it was successfully. So the question is about how can I know it the subscriptions webhook was fine.

This is my code:

const CLIENT_ID = "clientId";
const WEBHOOKS_SUBSCRIPTIONS = "https://api.twitch.tv/helix/webhooks/hub";
const USERNAME = "userName";
const USER_DATA = `https://api.twitch.tv/helix/users?login=${USERNAME}`;
const GET_TOKEN = 'https://id.twitch.tv/oauth2/token'
const CLIENT_SECRET_NOTIFICATION1 = 'clientSecretApp'
const LIST_SUBSCRIPTIONS = 'https://api.twitch.tv/helix/webhooks/subscriptions'

const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());


app.get("/v1/authorization", function (req, res) {
    const hubChallenge = req.query['hub.challenge']

    res.status(200).send(hubChallenge)

    // HERE how can I know if this send request was "ok"?
});

app.get("/", function (req, res) {
    res.send("Home")
    // Just to know if the server is running
})

app.listen(3000, function () {
    console.log("Example app listening on port 3000!");
});



 // METHODS ===================================================

const getListSubscriptions = bearerToken => new Promise((resolve, reject) => {
const { access_token } = bearerToken
  const config = {
    headers: {
    "client-id": `${CLIENT_ID}`,
    accept: "application/vnd.twitchtv.v5+json",
    "Authorization": `Bearer ${access_token}`
   }
}

axios.get(LIST_SUBSCRIPTIONS, config)
    .then(res => {
      resolve(res)
})
.catch(err => {
      console.log(err)
      reject(err)
    })
})

const getToken = new Promise((resolve, reject) => {
  const data = {
    client_secret: CLIENT_SECRET_NOTIFICATION1,
    grant_type: "client_credentials"
  }
  const dataJSON = JSON.stringify(data)
  const config = {
    headers: {
      'client-id': CLIENT_ID,
      'content-type': 'application/json'
    }
  }

axios.post(GET_TOKEN, dataJSON, config)
    .then(res => resolve(res.data))
    .catch(err => reject(err))
})

const subscribeHook = (userId, bearerToken) => new Promise((resolve, reject) => {
  const { access_token } = bearerToken
  const data = {
    "hub.callback": "url_accesible_from_internet/v1/authorization",
    "hub.mode": "subscribe",
    "hub.topic": `https://api.twitch.tv/helix/streams?user_id=${userId}`,
    "hub.lease_seconds": 10
  };
  const config = {
    headers: {
        "client-id": CLIENT_ID,
        "accept": "application/vnd.twitchtv.v5+json",
        "Authorization": `Bearer ${access_token}`
   }
};

axios
    .post(WEBHOOKS_SUBSCRIPTIONS, data, config)
    .then(resp => {
        resolve({
          status: resp.status,
          statusText: resp.statusText,
          data: resp.data
        })
   })
    .catch(err => {
      reject(err)
    })
})

const getUserId = new Promise((resolve, reject) => {
  const config = {
    headers: {
      "client-id": `${CLIENT_ID}`,
      accept: "application/vnd.twitchtv.v5+json"
    }
  };
  axios
    .get(USER_DATA, config)
    .then(resp => {
      resolve(resp.data.data[0]);
    })
    .catch(err => {
      reject(err);
    });
});

const init = async () => {
  const dataUser = await getUserId;
  const bearerToken = await getToken
  const isSubscribed = await subscribeHook(dataUser.id, bearerToken)
  const listSubscriptions = await getListSubscriptions(bearerToken)

  console.log(dataUser)
  console.log(bearerToken)
  console.log(isSubscribed)
  console.log(listSubscriptions.data)

};

init();

Sorry for the a lot of lines of code.
Regards!


#2

You can use the Get Webhook Subscriptions endpoint to see if a subscription is live https://dev.twitch.tv/docs/api/reference#get-webhook-subscriptions

Also, you don’t appear to have a POST handler so wont get any notifications as you have no way to handle incoming POST requests, so Twitch will repeatedly try to resend the notifications as you’re not responding to them.

Couple other things I noticed too, your subscribeHook function doesn’t actually seem to do anything, you just assign a few constants, you don’t actually resolve/reject the promise or actually do anything.

"accept": "application/vnd.twitchtv.v5+json" also doesn’t do anything at all in Helix, that’s a thing for v5, you shouldn’t be sending that for any Helix endpoints.


#3

Thanks so very much. Now I can see that there is a subscription active. Also, I added POST endpoint to get notifications and It is working,
here the code:

app.post("/v1/authorization", function (req, res) {
    console.log('there is a new notification')
    console.log(req.query)
    res.status(200).send('ok')
})

About function subscribeHook I wrapped into a Promise, and now return data

About the last point, “accept” I have just to delete this?


#4

Correct, you don’t need to send any Accept header for Helix endpoints so you can safely remove that.