Anvil: Web Apps with Nothing but Python Transcripts
Chapter: Adding APIs and HTTP Endpoints
Lecture: Hello world API

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


Talk Python's Mastodon Michael Kennedy's Mastodon