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. What we need to do is create our API endpoint. Now, we've come a long ways with our flask application over here,
0:10
and we've worked on our home view and we've had our design in place. But now we're ready to build the actual reason that this site exists,
0:18
at least for integration with twilio. And that is to do our order api, That is just basically a simple flask end-point that returns JSON
0:26
Now, the fact that it returns JSON is already what we want to do It just takes a Python dictionary and converts it over to JSON.
0:34
But what we want to do is we want to accept a JSON body as part of our API. Over in twilio studio,
0:42
we're going to come up with a JSON document that will be posted over to this end point. Right now, it responds to get, what we want to do is
0:51
we're going to go over here and say the methods = ['POST']. We wanted to just respond to post. So now, before I put this,
0:59
let's test it out here. If we go to /api/order remember, We got our JSON back here, but if we put this, obviously it's going to say no longer possible.
1:16
You have to do a post not to get, so that's going to be fine. Now we're going to get as I said, some sort of data posted over.
1:26
I'll call this just data and in flask, the way you get the body posted over as JSON. As you say, you go to the request,
1:33
which is always this sort of ambient thing. flask.request and you say get_json(force = True), if you want to force it,
1:42
even if it doesn't think the type is that and let's just return, let's just echo back the data we got, return{"received:" data}data itself.
1:52
We could just return data, but it's gonna be a dictionary. But just to show that we've kind of done something with it, we're gonna put it like this.
1:58
Now how do we call it? How do we pass something over to it? That brings us to a cool tool called a postman?
2:08
The over postman it allows you do much more controlled much more highly controlled requests do
2:15
various verbs we're gonna do a post over to http://localhost:5000/api/order. Singular. All right,
2:24
And then we have to pass a body over so we can go to Raw, Say it is JSON, JavaScript and we can just have name need,
2:34
double quotes, in JSON, "name":"Michael" and "location": "Portland", let's just say active is true because we want something other than
2:46
just strings passed over. All right, let's go do this post, see what we get. Look at that. We posted it over our application
2:53
received it, that a little receive that data said, Here's what we got. Well, it's just echoing it back, of course,
3:00
So we're not doing anything interesting yet, but our API endpoint is here and ready to receive JSON posted over from anywhere.
3:08
Right now, it's just coming from postman, but ultimately we're going to go to Twilio Studio to the flow and say at this
3:13
stage, when it's time to do in order, post all the information we've gathered through our chat, back to our order endpoint here