Loading
Current section: Real World Examples 15 exercises
lesson

Simplify API Interaction with AI Assistants

AI assistants are also great for letting you chat with an API based on a description of it.

In chat, provide the following prompt:


Write some code to make a fetch request to the API described here:
<paste description>

When you submit the prompt, the AI assistant will generate an example t

Loading lesson

Transcript

00:00 Another place I've had AI assistance help in that's been super helpful for me is when I'm given some like a bunch of language about how an API works and I just want to be able to talk to that API. So if I go to chat and here we'll do we can either do a quick chat right here that works just

00:17 fine and I'm going to go over to Slack and this actually came from a Slack message and I'm going to paste this in here and I'm going to say please make a or write some code to make a fetch request

00:32 to the API described here and then paste the description from my co-worker or my team member

00:45 in the chat here whatever they sent me from their Slack message and boom it just created a whole bunch of stuff even since I have one vulnerability which is like that's terrifying but we'll look at that here in a second. So we have this function to get for the get request

01:02 that's fetch API progress that's not quite right because we're running this server side so we're going to need to give the full domain URL for that but that's fine I can change that myself later and then device token yeah we'll have to get that ourselves that's fine it doesn't know anything

01:18 about how the device token shows up and then our response.json there as well and then here update progress yeah that API is probably okay and so yeah that works out pretty well now if I

01:33 want to go a little deeper in this then I'm going to pop open the chat view right here and I can make a couple of suggestions I'm going to click on this vulnerability we've got hard-coded credentials yeah we don't need to worry about that don't worry about that but yeah definitely check those out that's that is definitely handy and you can read the description here but that

01:53 that gives you a really good start and then you could take things a little bit further and say the domain for this API is hdps api.example.com please update the code for this and then I like

02:08 to take all of the different changes that I want to make and put it in a single message that way we're not like going back and forth I'm waiting for it to respond. So another response that I want to have it make is I want to have both of these accept an object that we destructure so

02:26 accept an object and destructure the named properties rather than accepting multiple parameters and let's see other things make well here let's let's hang on to the next one so I

02:46 also want to parse the result with a Zod schema and you can put that Zod schema together yourself and then provide that Zod schema here and it will parse it so in fact it will probably be able to just make the Zod schema for us so let's I can't remember exactly what we're supposed to expect

03:03 from these responses but let's say the progress API returns an array of items that have a slug

03:16 and a completed at date please parse the response from get progress using a Zod schema and let's see if it'll generate that for us as well and again it's just like talk back and forth with

03:33 your AI assistant if it didn't do it quite the way that you wanted it to you just make some adjustments and allow it to make those adjustments for you right here so let's say well what if I

03:45 want to like change from a require to an import statement okay so switch to import or ESM import

03:55 from CJS and let's say we also want to handle this error handle the error gracefully by returning

04:07 a error message an error message the AI doesn't actually care if you use proper grammar and it will just update the code for us now that's also not quite the way I would do it and so we can

04:22 continue on this path and say let's do import Z from Zod instead and that will update and it will continue like you just keep on iterating with this AI assistant and then finally when you're ready to

04:40 go you can copy this to your clipboard put it in your editor and run it and make sure that it you know is doing what you expected but this is a lot faster than just managing all of this stuff yourself you can have your little intern your AI assistant generate all of this stuff for you

04:59 and improve your productivity while you're at it so there you go