Joey Coder
Joey Coder

Reputation: 3489

Django: Create webhook receiver

I am currently trying to implement webhooks for this site. I can't find much in the documentation about creating a webhook. Do you have any good repositories or pages I can look into to get a better understanding of how to build a webhook for Typeform?

Upvotes: 5

Views: 7561

Answers (1)

picsoung
picsoung

Reputation: 6499

As Daniel pointed out in their comment, a webhook receiver is just another endpoint in your Django app, accepting POST requests and dealing with JSON input.

I tried to put together an example, hope it helps.

import json
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_POST

@csrf_exempt
@require_POST
def webhook_endpoint(request):
    jsondata = request.body
    data = json.loads(jsondata)
    for answer in data['form_response']['answers']: # go through all the answers
      type = answer['type']
      print(f'answer: {answer[type]}') # print value of answers

    return HttpResponse(status=200)

Upvotes: 14

Related Questions