Python-powered chat apps with Twilio and SendGrid Transcripts
Chapter: Creating the Flask Python web app
Lecture: Building the API order endpoint
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
In order to integrate with Twilio.
0:02
What we need to do is create our API endpoint.
0:05
Now, we've come a long ways with our flask application over here,
0:09
and we've worked on our home view and we've had our design in place.
0:13
But now we're ready to build the actual reason that this site exists,
0:17
at least for integration with twilio.
0:19
And that is to do our order api,
0:20
That is just basically a simple flask end-point that returns JSON
0:25
Now, the fact that it returns JSON is already what we want to do
0:30
It just takes a python dictionary and converts it over to JSON.
0:33
But what we want to do is we want to accept a JSON body as part
0:38
of our API. Over in twilio studio,
0:41
we're going to come up with a JSON document that will be posted over to this
0:45
end point. Right now, it responds to get, what we want to do is
0:50
we're going to go over here and say the methods = ['POST'].
0:55
We wanted to just respond to post.
0:57
So now, before I put this,
0:58
let's test it out here. If we go to /api/order remember,
1:06
We got our JSON back here,
1:09
but if we put this, obviously it's going to say no longer possible.
1:15
You have to do a post not to get,
1:18
so that's going to be fine.
1:20
Now we're going to get as I said,
1:22
some sort of data posted over.
1:25
I'll call this just data and in flask,
1:28
the way you get the body posted over as JSON.
1:31
As you say, you go to the request,
1:32
which is always this sort of ambient thing.
1:35
flask.request and you say get_json(force = True),
1:38
if you want to force it,
1:41
even if it doesn't think the type is that and let's just return,
1:45
let's just echo back the data
1:46
we got, return{"received:" data}data itself.
1:51
We could just return data, but it's gonna be a dictionary.
1:53
But just to show that we've kind of done something with it,
1:56
we're gonna put it like this.
1:57
Now how do we call it?
1:59
How do we pass something over to it?
2:01
That brings us to a cool tool called a postman?
2:07
The over postman it allows you do much more controlled much more highly controlled requests do
2:14
various verbs we're gonna do a post over to http://localhost:5000/api/order.
2:20
Singular. All right,
2:23
And then we have to pass a body over so we can go to Raw, Say
2:27
it is JSON, JavaScript and we can just have name need,
2:33
double quotes, in JSON, "name":"Michael"
2:39
and "location": "Portland", let's just say active is true because we want something other than
2:45
just strings passed over. All right,
2:47
let's go do this post, see what we get.
2:49
Look at that. We posted it over our application
2:52
received it, that a little receive that data said,
2:55
Here's what we got. Well,
2:56
it's just echoing it back, of course,
2:59
So we're not doing anything interesting yet,
3:01
but our API endpoint is here and ready to receive JSON posted over from anywhere.
3:07
Right now, it's just coming from postman,
3:08
but ultimately we're going to go to Twilio Studio to the flow and say at this
3:12
stage, when it's time to do in order,
3:14
post all the information we've gathered through our chat, back to our order endpoint here