download-icon
INPUT VARIABLES
{
shopDomain
}
The Shopify store domain (e.g., `your-development-store.myshopify.com`).
{
accessToken
}
The Shopify access token.
{
orderId
}
The ID of the order to fetch.
{
}
{
}
{
}
share-icon
OUTPUT VARIABLES
{
OrderResponse
}
Entire JSON response from Shopify.
{
orderLineItems
}
All products of that order joined with a comma
{
orderTotalPrice
}
{
orderId
}
{
}
{
}
paths-icon
PATHS
{
success
}
{
error
}
{
}
{
}
{
}
{
}

Function Walkthrough

Function Code Snippet

 
export default async function main(args) {
    const logs = [];

    // Extract input variables from args
    const { orderId, accessToken, shopDomain } = args.inputVars;
    logs.push(`Extracted inputs - orderId: ${orderId}, accessToken: ${accessToken ? 'PROVIDED' : 'MISSING'}, shopDomain: ${shopDomain}`);

    // Validate that the required input variables are provided
    if (!orderId || !accessToken || !shopDomain) {
        logs.push("Error: Missing required input variables: orderId, accessToken, or shopDomain");
        return {
            next: { path: 'error' },
        };
    }

    // Define the endpoint URL
    const url = `https://${shopDomain}/admin/api/2024-01/orders/${orderId}.json`;
    logs.push(`URL: ${url}`);

    // Configure the fetch request
    const config = {
        method: 'GET',
        headers: {
            'Content-Type': 'application/json',
            'X-Shopify-Access-Token': accessToken,
        },
    };

    try {
        // Make the API call
        logs.push(`Making API call to URL: ${url}`);
        const response = await fetch(url, config);

        // Check if the response status is OK
        if (!response.ok) {
            throw new Error(`HTTP error! status: ${response.status}`);
        }

        // Extract the JSON body from the response
        logs.push("Extracting JSON from the response");
        const responseBody = await response.json;

        // Validate the responseBody structure as expected
        if (!responseBody || !responseBody.order || typeof responseBody.order !== 'object') {
            throw new Error("Invalid or missing response body from the API");
        }

        // Extract required order details
        const order = responseBody.order;
        const orderId = order.id;
        const orderTotalPrice = order.total_price;
        const orderLineItems = order.line_items.map(item => item.name).join(", ");

        logs.push(`Order data retrieved: ID - ${orderId}, Total Price - ${orderTotalPrice}, Line Items - ${orderLineItems}`);

        // Create the success return object with extracted data
        return {
            outputVars: {
                OrderResponse: JSON.stringify(responseBody),
                orderId,
                orderTotalPrice,
                orderLineItems
            },
            next: { path: 'success' },
        };
    } catch (error) {
        logs.push(`Error: ${error.message}`);
        return {
            next: { path: 'error' },
        };
    }
}
copy-icon

Have something to share?

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

ghraphic
No items found.