Anvil: Web apps with nothing but Python Transcripts
Chapter: Adding APIs and HTTP Endpoints
Lecture: Hello world API
0:00 It may feel like we've already built an API
0:02 cause if you remember, when our code over here runs
0:09 on the browser, and this server code like what we have here
0:13 this anvil.server.callable, this runs on the Anvil servers
0:17 and there's a communication across them
0:20 kind of like a service or API would.
0:22 This callable is not really for building external services.
0:26 It's for gluing the server side
0:28 and the front-end side of Anvil together.
0:32 If we want to have a proper stand-alone service
0:35 that folks can use, we need to do something different.
0:38 So let's go down here
0:39 and create something we are going to call an API.
0:42 Now, Server Module 1 is exciting
0:44 but I prefer the name, just flat api.
0:48 And at first glance, there's nothing here that would
0:50 indicate to you that you might be able
0:53 to use some kind of service.
0:55 So what you can do is come down here
0:57 and you can do anvil.server.callable just like we saw.
1:01 But like I also said, this is not what you want.
1:04 So, we're going to do something different.
1:05 Let me just clean up a few things.
1:06 We don't need drives, probably need tables.
1:09 Yeah, that's probably good.
1:11 So, what we're going to use is something different here.
1:14 And it's going to be an HTTP endpoint.
1:17 And if you look at the parameters here
1:18 the required thing you pass in is the path.
1:21 This is like a sub part of the URL
1:24 like basically the end part of the URL.
1:26 And you can also say whether it requires credentials
1:28 authenticates users, whether it does get or post or both.
1:34 CORS is Cross Origin Request Security
1:36 and then Cross Site Session and so on.
1:38 Things like this, okay?
1:39 So, we're going to say that
1:40 and what I want to just set here is the path.
1:43 And let's just have a super simple thing
1:46 that just says, Hello. Alright, it's not going to take
1:48 any parameters or anything like that.
1:51 And instead of printing, it's going to return
1:54 let's say, a JSON document that says
1:56 Message: Hello from the API.
1:59 Now we can go and call this.
2:01 When we run this up here we get this quite funky URL
2:06 and that's not something we can use for this.
2:09 Okay, I got to put a little slash here.
2:11 This is not what we can use here.
2:13 We have to use a different URL and if you look at the bottom
2:15 you get an even more crazy-looking URL here.
2:18 That's fine, you technically could use that
2:21 but what we can do is if we go back here to our app
2:23 real quick and we say publish app
2:26 we're not really going to publish it
2:27 but we can say we'd like to be able to share it
2:29 through a public link and it will come up with something
2:32 like fresh-zealous-song.anvil.app.
2:35 We can also add a custom domain, which we'll do later
2:38 but for now we can at least get this public URL here.
2:42 We hit okay, then notice down here
2:44 we have a much better looking URL that we can share
2:48 more stable, things like that.
2:50 Let's just put, ah, Hello, maybe the same name
2:53 or something like that.
2:54 This is not actually what we're trying to build
2:56 but we're just going to build a simple, little API
2:58 just to test it to see that it works.
2:59 We don't have to run the app, just as long as we save it
3:02 we should be able to request it.
3:04 So, let's try that.
3:06 If we go here and then, what did I say?
3:08 Hello...yes, hello. So, let's try that.
3:11 Okay, that, we got some JSON back.
3:14 If we look at the raw data, it's message is
3:16 "Hello, from the API." How cool is that?
3:19 This is what we have to do to create an API in Anvil.
3:22 We convert the callable to an HTTP endpoint.
3:25 We probably want to at least give it a stable, semi-public
3:29 endpoint here that we can get to
3:31 and then we write a function.
3:32 The function doesn't take any parameters.
3:34 We'll see how to pass data to it in just a moment.
3:36 And then we return some kind of dictionary
3:39 which is converted directly to JSON.
3:41 The Python dictionary is in JSON, our super similar
3:44 like, single quotes first, double quotes basically
3:46 as long as all the elements are serializable in JSON.
3:48 And this is a really easy way to get started.