Back to all functions

Search and retrieve a GIF from GIPHY

Searches Giphy for gifs matching the specified searchTerm with a given language.

Created By
Denys Linkov
Community
download-icon
INPUT VARIABLES
{
searchTerm
}
Keywords to search for GIFs
{
Language
}
A two letter code, like 'en'
{
apiKey
}
API key for Giphy (see documentation)
{
limit
}
Number of GIFs to retrieve from Giphy
{
}
{
}
share-icon
OUTPUT VARIABLES
{
}
{
}
{
}
{
}
{
}
{
}
paths-icon
PATHS
{
Success
}
success path
{
Error_Fetching
}
Error path
{
}
{
}
{
}
{
}

Function Walkthrough

Function Code Snippet

 
export default async function main(args) {
    let { language, searchTerm, apiKey, limit } = args.inputVars;

    try {
        if (!language) { language = 'en' }
        if (!searchTerm) { throw new Error("missing a value for `searchTerm`"); }
        if (!apiKey) { throw new Error("missing API key"); }
        if (!limit) { limit = 1; }
    } catch (err) {
        return {
            next: {
                path: 'Error_Fetching'
            },
            trace: [
                {
                    type: "debug",
                    payload: {
                        message: `Could not search for a .gif, ${err.message}`
                    }
                }
            ]
        };
    }

    const apiUrl = `https://api.giphy.com/v1/gifs/search?api_key=${apiKey}&q=${searchTerm}&limit=${limit}&lang=${language}`;
    const response = await fetch(apiUrl);
    const responseBody = response.json;
    return {
        next: {
            path: 'Success'
        },
        trace: responseBody.data.map(properties => ({
            type: "cardV2",
            payload: {
                imageUrl: properties.images.fixed_height.url,
                title: properties.title,
                description: {
                    text: properties.title,
                }
            }
        }))
    };
}
copy-icon

Have something to share?

Share your creation with over 250,000 other global Voiceflow users.

ghraphic
No items found.