Search code examples
apicurlhttprequesttwilioapigee

How to bridge from a webhook URL to a HTTP POST request?


I want to make a HTTP POST request to Twilio but the calling service only allows me to enter a webhook URL.

I was trying to bridge this with apigee's API proxy but I could not figure out how to make it work.

The flow is like this: A chat bot on motion.ai calls a web hook URL at a certain point. The call should make an outbound call via twilio.com which requires a HTTP POST request, see here.

The POST request looks like this:

$ curl -XPOST https://api.twilio.com/2010-04-01/Accounts/<...>/Calls.json \
    --data-urlencode "Url=http://demo.twilio.com/docs/voice.xml" \
    --data-urlencode "To=<...>" \
    --data-urlencode "From=<...>" \
    -u '<...>:<...>'

What is the easiest way to bridge this?


Solution

  • I managed to setup an API proxy with Apigee to convert the HTTP GET request to a HTTP POST request.

    Create a API proxy in Apigee and add a Basic Authentication policy:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <BasicAuthentication async="false" continueOnError="false" enabled="true" name="Basic-Authentication-1">
        <DisplayName>Basic Authentication-1</DisplayName>
        <Operation>Encode</Operation>
        <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
        <User ref="request.queryparam.username"/>
        <Password ref="request.queryparam.password"/>
        <AssignTo createNew="false">request.header.Authorization</AssignTo>
        <Source>request.header.Authorization</Source>
    </BasicAuthentication>
    

    Next add a Assign Message policy:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <AssignMessage async="false" continueOnError="false" enabled="true" name="Assign-Message-1">
        <DisplayName>ConvertQueryToFormParameters</DisplayName>
        <Properties/>
        <Copy source="request">
            <Headers/>
            <QueryParams/>
            <FormParams/>
            <Payload/>
            <Verb/>
            <StatusCode/>
            <ReasonPhrase/>
            <Path/>
        </Copy>
        <Add/>
        <Set>
            <FormParams>
                <FormParam name="To">{request.queryparam.To}</FormParam>
                <FormParam name="From">{request.queryparam.From}</FormParam>
                <FormParam name="Url">{request.queryparam.Url}</FormParam>
            </FormParams>
            <Verb>POST</Verb>
        </Set>
        <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    </AssignMessage>
    

    Then you can make a POST request to Twilio by simply calling https://<yourApigeeApiUrl>.apigee.net/<yourApiName>?username=<yourTwilioApiUsername>&password=<yourTwilioApiPassword>&...