Connecting an External API with Fastgen

Ray Deck builds a FastGen API endpoint that forwards requests to an external astrology API on RapidAPI and returns the result. He configures the HTTP request with custom headers, maps the response using dot-notation, tests it in the debugger, and deploys it for public access via Postman.
Chapters
So let's talk about how to do an external API request in FastGen. And we're gonna, you know, there's a whole universe of external APIs that we can connect to. And this actually comes from a request from the FastGen community where we're hooking into this astrology API for whichever reason. And we're going to talk about how to make connection to that and do that all within FastGen. Now, FastGen thinks in terms of flows. Flows. It's really workflow-centric. And there are two major kinds of workflows within FastGen. There's going to be what they call workflows, which are asynchronous, right? So the, you can, the big use cases for workflow are going to be when you are, you know, largely scheduling like a cron event, as developer would call it, but just like something you want to fire off every day or every hour or whatever. And, or actually, it's also kind of useful for being able to respond to emails, which is a neat little feature that FastGen has built into it. And then you can also, but what they both have in common is that you will run them asynchronously. And that means that it won't actually respond to whoever just called it. So an email is, I got an email, maybe later you respond to it, but really all you do is catch it. So there's no response required in that transaction. A cron-based event doesn't have a reply because it's a clock telling you what to do. So all of these are asynchronous and have more in common like with a Zapier workflow. Than they do with an API that actually is doing call and response. So the other major type of workflow we're going to be able to use is called an API. An API has a response to it. And you know, that's where you go hit something on a web page, or web page calls a backend, asks for data, and then gets that data back. The "and gets that data back" part, that's what separates an API from a workflow. And what we're going to do today is we're going to make an API that calls an API, right? So our API here here in FastGen is going to call the API in the astrologer and be able to, you know, get data from that and then return that back to the front end. We'll be able to put these pieces together. So how do we do that? We do that by clicking Add Route. We'll make it a GET, nice and simple. And we'll need to give a name, and the name will be rapid, you know, astrology YouTube test. Cool. And then we're gonna make it a GET and that's fine. And we'll call it, you know, astrology. Okay. So now if I hit the FastGen thing at astrology, it would run this particular endpoint. Another key thing is that most things in FastGen are secured. That's generally a good idea. But what we're gonna do is make this into a public route just to make it easier to test and not have any authentication required. So when you're initially testing, you might find that you need to set up this kind of routing in order for it to work properly. Cool. So let's, let's add on an HTTP request. HTTP is protocol of the web. That is the way that you would talk to other websites. HTTPS, which you usually see at the beginning of your schemes for websites. If I were to go click on this, you see HTTPS up here. The S is an additional modifier to HTTP that makes it secure. And really, that's what we're gonna need to use here. So we're going to go get something from that particular API. At this point, we need to ask the API for a little help. We need to know how to talk to it. And the easiest way to do that is going to be through going to the documentation and having it tell us what it is that we need. And here we can say this is just RapidAPI and we're gonna need a key and we're gonna need a host. So in order for us to use RapidAPI, we're going to need to sign up and what they call subscribe to the given API because you see these APIs are paid., and the, for anything more than totally trivial response. So having logged in, uh, the next thing we need to do is we need to, uh, subscribe to this particular API, and that's going to be what allows them to start collecting, collecting money, although we're going to go on to the free plan for this particular demo. So if I click subscribe to test, I can say, yep, and we will choose this basic zero, and you can see this is limited to 15 requests per month, so it'll be useful for this YouTube, and it's not gonna be useful for anything after that. So the keys, should you see them in the course of this demonstration here, aren't gonna be very helpful because they're gonna be keys for the test plan. Alright, subscribe. Alright, cool. Now we can go back to the Astrologer endpoints. And here we are. So we're going to do is pay attention now to how it is we're supposed to go talk to this thing. And the— we're going to need to get a URL. Okay, well, here's the URL. We're going to copy that and we're going to go over to FastGen. We're going to paste it in. Okay, so remember, this is for our HTTP requests that we're doing. We're going to say call rapid API astrologer. FastGen really likes you to type in those names. It does encourage self-documentation, so I sort of get that, but it can be annoying when you forget about it. It's like these things just don't work until you do that. And then we're going to need to add headers. Now, it is an authenticated API over on the RapidAPI side, but really using— it doesn't use basic or bearer, it uses a custom header. So let's just pay attention to what those custom headers are. So one is called RapidAPI key. I like that. We'll go over here and we'll paste in this header. Oh no, that didn't work at all. Let's see it highlight everything. Copy. I'm just gonna see here. Okay, click it. Paste. Yep, cool. And then the value of that is going to be this. Paste. Now, in general, I'm going to encourage that we use this kind of key in FastGen environment variables, but we'll save that for another time. So that's gonna be one header that we need. The other header we need is going to be called rapidapi_host. Copy, paste, copy, paste. Now we've got the GET, we have the header that it requires, and there's kind of nothing else to it. It's relatively simple at that point. Okay, so now we've got this data, what should happen is the data will be collected into, you know, the body for this particular action. So I'm going to just click out of this for a second. You'll see this stuff is still there, just because it auto-saves most of the time. And then we can give it a response. This response is important for making this a real API, because response is how it knows what to send back to the front end. And you're going to find that a lot of your APIs you build within FastGen are going to be do a thing and then respond with the answers. Then how are you supposed to respond? This is something that's not my totally favorite about FastGen, but, but once you get the hang of it, it's not super crazy. You will need to create for yourself the JSON for it. And the way you create the JSON is you're going to say response, right? And then the response is going to be, and you put in quotation marks. And then within those quotation marks, you insert a variable. And this is gonna be where I'm using the action call rapidapi/astrologer. That's the thing we just created. I also happen to know that when this thing runs, it's going to have something called body within it, which is the, which is the container for the data that comes back from RapidAPI. We may want to actually filter that even more. But the way you pick off children on this is going to be through using dot syntax. So rapidapi, da da da. .aswalger.body. If you sort of expand it out here, you can see more about it. By the way, I just use this expander and contractor up here in the upper right-hand corner. That looks good to me. Uh, you'll also notice here it has execute actions after response. That's so that you can have it run additional things afterwards. Otherwise it cuts off the flow with, with success because, well, when you send back the answer, you're done usually, right? The answer is usually. Sometimes there's more to do, particularly AI-based systems are going to start to use that kind of feature, but not the kind of thing you need to worry about right now. Okay, let's give it a name. And remember, like I said, they require all these things to be labeled. Return body. And you'll also notice FastGen's awful habit of often forgetting what you wrote. Response. And See now his name and it put the thing in. I'm pretty sure that I clicked away without the body. That's the reason why I didn't remember it. Um, exactly why it does that is not totally clear to me. So when you click away from something, before you do that, copy what you just created. And then when you come back, if you, if it had blown away, you just do a paste back again. Uh, such is the price we pay for working with this cutting-edge technology. Okay, let's go test. We're gonna test request. And it did a green here, where it says nothing came in the door. All right. And then here, this is the response that came back. I'm just gonna expand a little bit so we can see actually what came in. So this is action called Rapid Astrologer. You see it has body, and body has data. Over here on the left-hand side, if you put your mouse over there, you'll see these things start to collapse, which is actually super handy. To be able to better surf through this, this longer stuff. That's because this whole thing is built on what's called a Monaco editor, which is technology from Microsoft, the same thing that's used in Visual Studio Code. And you're gonna find a lot of conventions you use here are going to be conventions you can, you can apply to all sorts of other, you know, low-code-ish or even pro-code-ish environments. You'll see that the main 3 things that come back from a response are the body, the header, and the status. The status 200, it means it was successful. Bad statuses include like 500, 401, that kind of thing. The header is usually what it sent back to you and whether there's more for it to worry about. In our case, there's nothing very interesting. And then the body is usually where the really good stuff is. And that's going to be the status OK and the data, I think, that came back from the API that's sort of sitting behind that RapidAPI gateway. Part of what this tells me is that we might be more interested in the in the, in the data as opposed to being in the body, but we'll get to that in just a second. And we can see that it has sent us back all sorts of good data. All right, but this so far is not what came back from the API. This so far is just what that one step does. Then we need to ask what's getting sent back in the return body. You can see the return body right now has a response. And again, we'll expand it just so we can understand it better. Whenever you're dealing with this large data, just give yourself more room. Don't try to Don't try to look at the room through a keyhole here. And you see a response, okay? It has data, and it has a status. And like, that's, I gotta say, that's a little bit higher level of abstraction than we really require here. So what I'm thinking is we probably want to pick it off at the data level, 'cause really it's only stuff inside data that's gonna be most interesting to us. So we're going to update in order to do that. And to do that, we're gonna go exit debug mode, go back to the return body. Remember, the HTTP request has now done its job. It's gotten all the data we need to get. What we want is to get the return body. And we're going to say.body.data. And we can have a fallback data of error. Right. So now you can start to see how it sort of generates its own little pseudocode thing to be able to say what the, what the response is going to be. Okay, so let's test it out again. So we tested it, it just ran test for us just now. And I can take a look, it came in with no information, it got back probably the same data because it's just astrology. And now you can see the response directly includes Chiron and all that other good, good stuff here. Okay, now we have something that we like and that we like to try running outside of this particular environment. So what we're gonna do is we're gonna exit debug mode and we're going to click deploy. Now when we deploy it and deploy it to live, uh, this will make the site actually accessible to us. In order for us to test the site, we're gonna use this link up here where it says /astrology. So we'll copy that and then we can use Postman. And what we want to do is actually just send another request. We're gonna create a new request and here we're going to call the FastGen API. And to do that, we go back over to FastGen. You see where it has, shows this little link here, we click that little link, go back over to here and paste that in. And actually, this doesn't require anything else. We didn't set up any authentication. We haven't done anything fancy with FastGen. So we're just going to say send. See, and we've got all sorts of wonderful data coming back to us. So now we have set ourselves up with a nice simple FastGen endpoint that has a job of communicating with RapidAPI, sends out those custom headers, knows how to get the URL,, and then ships all that over to, um, you know, RapidAPI, collects the information back, and then marshals that information into a response that can be sent back and consumed by whatever front end you choose. Uh, there's obviously much more to it. There are POST requests, there's authentication, there's, um, you know, making use of environment variables. Those are things we just talked about so far because as we start to build these, uh, systems, they get more complicated. But hopefully this, uh, gives you a little bit of introduction to how we get a little bit of value today out of FastGen. Uh, and, uh, one of the big uses I think of this is just being able to have a secure gateway, right? So that your front end is able to call your backend and then your backend is using the key to go talk to whatever your third-party services are. That third-party service could be RapidAPI, Astrologer API. Uh, it could be talking to OpenAI. It could be talking to, uh, whatever service is gonna be adding value for you. But this core process of receive a request, you know, forward, transform, and then send it back is the essence of a lot of backend work. And it can be pretty easily done with FastGen, much in the manner we just described. I hope this is helpful, and I'll see you next time.










