Python-powered chat apps with Twilio and SendGrid Transcripts
Chapter: Creating the Flask Python web app
Lecture: Concept: Creating an HTTP JSON API

Login or purchase this course to watch this video and the rest of the course contents.
0:00 We saw how to call an API from Twilio studio.
0:03 But how do we create one?
0:04 Well, we're going to use python and flask,
0:07 and it's really, really straight forward,
0:10 but we're going to define a python function called cake_order().
0:13 You call it whatever you want.
0:14 This name actually is only needs to be making sense.
0:17 You it's not used publicly. We're going to add a route.
0:21 And we used to blueprints this properly,
0:23 separate our code and not just jam it into one giant file.
0:26 So blueprint.route give it the URL until it accepts posts.
0:30 Remember? That's what we were doing over in studio.
0:32 We said we're going to post JSON body,
0:34 so make sure we set it to be post.
0:36 Then we need to get the data submitted.
0:37 My twilio studio. So we say flask,
0:40 request get JSON. That gives us a python dictionary equivalent of what we had over
0:44 there Is that JSON document, we used pydantic to convert and validate that model that data
0:50 So we created an order model and it had a cake and a customer and
0:54 a price and so on. So we just take the data and this double *
0:58 thing means take a dictionary and converted to keyword arguments, passed to the constructor or initializer
1:03 here that generates the model data,
1:06 which is all validated and verified.
1:08 We had this user service which recorded the order and created the user if necessary.
1:12 So we passed over things like the cake customer and price.
1:16 And then finally we create a response because,
1:18 remember, Twilio Studio wants to say,
1:20 thanks that worked, your order was accepted and your orderid is 700 or whatever it
1:25 is. So then we're going to pass that back,
1:27 and because we want to have a dictionary and not a pydantic models return,
1:32 we have to say response.dict And that's all we do.
1:35 This is all of it, except it's not because maybe something goes wrong.
1:39 Maybe they submitted the wrong data and that model validation that order model **data
1:43 Maybe it says, Whoa,
1:45 the customer part is missing. That's not going to work.
1:48 That would look like our service crashed.
1:49 But the problem really is what they submitted right.
1:52 In order to communicate that back,
1:54 we put this into a try except section,
1:57 and then we send back some kind of error here.
1:59 We're just saying error code 500.
2:01 But we could also catch a validation error out of pydantic and say,
2:04 That's a 400 or 422,
2:06 you know, invalid data was passed in all sorts of things like that.
2:10 We could have different error exceptions and send those back.
2:12 But the main takeaways come up with their body,
2:14 come up with the error code.
2:16 That's the status code. And then we just say,
2:18 flask.response. Give it the JSON has text,
2:21 set the status code, set the mimetype application,
2:24 JSON, and send it right back to them.
2:27 That's it. That's the entire order,
2:29 API implementation. And this is how studio and our python flask application work
2:35 together to hand of the ordering process.