Search code examples
pythondjangotypeform

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?


Solution

  • 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)