Google action fetching data from an API

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
In the previous article, we looked at fetching data from an API for our Google action. However, in my case, I don't have an API at my disposal. I have an RSS feed, so let's see how we can use that to fetch the latest article, which we can return. If ...
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

Now that we have our action triggered programmatically let's see how we can get the response data from an API.
This way, we can make it a bit more interactive.
If you'd like to follow along, please complete the previous article first.
The cool part now that we have our action using a function is that we can use all kinds of node goodies. One of these goodies is the fetch API.
Let's use a placeholder API first, so it's easier to test.
Open up your action and start by adding the fetch API.
const fetch = require('node-fetch');
Then inside our greeting function, we should convert it to an async function.
// Previous
app.handle('greeting', (conv) => {});
// New
app.handle('greeting', async (conv) => {});
Now that our function is asynchronous, we can await the fetch; since it's direct JSON we can await the JSON conversion as well like this:
app.handle('greeting', async (conv) => {
const response = await fetch('https://jsonplaceholder.typicode.com/posts/1');
const text = await response.json();
});
And then, we can modify our response card to return this data instead of the static data.
app.handle('greeting', async (conv) => {
const response = await fetch('https://jsonplaceholder.typicode.com/posts/1');
const text = await response.json();
conv.add(
new Card({
title: text.title,
text: text.body,
image: new Image({
url: 'https://daily-dev-tips.com/images/logo.png',
alt: 'Daily Dev Tips logo',
}),
})
);
});
You can now save and deploy your function, be sure to wait a minute or so for it to deploy. Then test your function and see it in action!

And that's it. We now added an API call to our Google action! Pretty cool stuff, if I may say so.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter