Match - via Webhook

Match - via Webhook

Match information available through the Match API endpoint, automatically updated and delivered to your Webhook URL in real-time.

Configuring your Console

Follow the instructions below to receive automated cricket match updates in your Webhook URL.
  1. Login to your Roanuz Sports Console and from the Dashboard, click on Push Configuration and select the Webhook Option.
  2. Now, in the text boxes, paste your Webhook URLs to which you want the data to be delivered automatically.
  3. Make sure to set up your webhook URL to handle the data correctly. A sample code has been provided under the “Handling the data received” section.

Send a Subscription Request

Once your console is set up, you will have to subscribe to the matches individually for receiving automatic updates. The sample snippet below demonstrates how to send a subscription request from the Match Subscribe API. Please note that subscribing twice to the same match does not mean that you will be charged twice, or you will receive the same update twice.

Match Subscribe - Webhook

terminal
pip install requests
pip install requests
icon copy
Python
import requests project_key = 'YOUR_PROJ_KEY' token = 'YOUR_ACCESS_TOKEN' match_key = 'ausind_2020_odi01' url = "https://api.sports.roanuz.com/v5/cricket/{}/match/{}/subscribe/".format(project_key,match_key) headers = { 'rs-token': token } payload = { 'method': "web_hook" } response = requests.post(url, headers=headers, json=payload) print(response.json())
import requests

project_key = 'YOUR_PROJ_KEY'
token = 'YOUR_ACCESS_TOKEN'
match_key = 'ausind_2020_odi01'
url = "https://api.sports.roanuz.com/v5/cricket/{}/match/{}/subscribe/".format(project_key,match_key)
headers = {
    'rs-token': token
}
payload = {
    'method': "web_hook"
}
response = requests.post(url, headers=headers, json=payload)

print(response.json())
icon copy

Handle the data received

Once successfully set up, Roanuz Cricket API servers will automatically start pushing data to your systems as the match progresses. Below are sample snippets demonstrating how to handle the data once it is pushed by the server in both Python and Node.js.

1. Python (Flask)

For developers using Python, here's how you can handle the incoming match data:
TERMINAL
pip install flask
pip install flask
icon copy
webhook_handle_data.py
from flask import Flask, request import gzip import io import json app = Flask(__name__) @app.route('/', methods=['POST']) def hello(): print("Received") c_data = io.BytesIO(request.data) text_data = gzip.GzipFile(fileobj=c_data, mode='r') byte_str = text_data.read() dict_str = byte_str.decode("UTF-8") mydata = json.loads(dict_str) print(mydata) print("____") return 'Hello, World! {}'
from flask import Flask, request
import gzip
import io
import json
app = Flask(__name__)


@app.route('/', methods=['POST'])
def hello():
    print("Received")
    c_data = io.BytesIO(request.data)
    text_data = gzip.GzipFile(fileobj=c_data, mode='r')
    byte_str = text_data.read()
    dict_str = byte_str.decode("UTF-8")
    mydata = json.loads(dict_str)
    print(mydata)
    print("____")
    return 'Hello, World! {}'
icon copy

2. Node.js (Express)

For developers using Node.js, here's how you can handle the data:
TERMINAL
npm install express body-parser zlib
npm install express body-parser zlib
icon copy
webhook_handle_data.js
const express = require('express'); const bodyParser = require('body-parser'); var zlib = require('zlib'); const app = express(); const port = 5000; function rawBody(req, res, next) { req.rawBody = ''; req.chunks = []; req.on('data', function(chunk) { req.chunks.push(Buffer(chunk)); }); req.on('end', function() { next(); }); } app.use(bodyParser.json()); app.use(rawBody); const API_KEY = "RS_API_KEY"; app.post('/match/feed/v1/', function(req, res) { var buffer = Buffer.concat(req.chunks); if (req.headers['rs-api-key'] == API_KEY) { zlib.unzip(buffer, (err, buffer) => { if (!err) { data = JSON.parse(buffer.toString()); console.log("Data", data, typeof(data)); res.send(JSON.stringify({"status": true})); } else { res.send(JSON.stringify({"status": false})); } }); } else { console.log("Auth Failed"); } }); app.listen(port, () => console.log(`Example app listening on port ${port}!`));
const express = require('express');
const bodyParser = require('body-parser');
var zlib = require('zlib');

const app = express();
const port = 5000;

function rawBody(req, res, next) {
    req.rawBody = '';
    req.chunks = [];
    req.on('data', function(chunk) {
        req.chunks.push(Buffer(chunk));
    });
    req.on('end', function() {
        next();
    });
}

app.use(bodyParser.json());
app.use(rawBody);

const API_KEY = "RS_API_KEY";

app.post('/match/feed/v1/', function(req, res) {
    var buffer = Buffer.concat(req.chunks);

    if (req.headers['rs-api-key'] == API_KEY) {
        zlib.unzip(buffer, (err, buffer) => {
            if (!err) {
                data = JSON.parse(buffer.toString());
                console.log("Data", data, typeof(data));
                res.send(JSON.stringify({"status": true}));
            } else {
                res.send(JSON.stringify({"status": false}));
            }
        });
    } else {
        console.log("Auth Failed");
    }
});

app.listen(port, () => console.log(`Example app listening on port ${port}!`));
icon copy

Unsubscribe from a Match

For any reasons, if you wish to opt-out of automatic updates for a match, you can do so by placing an unsubscribe request from the Match Unsubscribe API.

Match Unsubscribe - Webhook

TERMINAL
pip install requests
pip install requests
icon copy
match_unsubscribe_webhook.py
import requests project_key = 'YOUR_PROJ_KEY' token = 'YOUR_ACCESS_TOKEN' match_key = 'ausind_2020_odi01' url = "https://api.sports.roanuz.com/v5/cricket/{}/match/{}/unsubscribe/".format(project_key,match_key) headers = { 'rs-token': token } payload = { 'method': "web_hook" } response = requests.post(url, headers=headers, json=payload) print(response.json())
import requests

project_key = 'YOUR_PROJ_KEY'
token = 'YOUR_ACCESS_TOKEN'
match_key = 'ausind_2020_odi01'
url = "https://api.sports.roanuz.com/v5/cricket/{}/match/{}/unsubscribe/".format(project_key,match_key)
headers = {
    'rs-token': token
}
payload = {
    'method': "web_hook"
}
response = requests.post(url, headers=headers, json=payload)

print(response.json())
icon copy