Hire 
Daniel D'Souza
Submit
Note: Voiceflow is not responsible for the exchange of services between you and the partner.
Thank you! An email will be sent connecting you with the expert.
Oops! Something went wrong while submitting the form.

Function Template (API Call)

Template
Function
51
Template
Function
by
Daniel D'Souza

This is a template you can use to create a function that uses an API call. It uses the Zendesk create ticket API as an example.

Created:

Heading

Voiceflow APIs used:

Channels
No items found.
Created By
Daniel D'Souza
This is some text inside of a div block.

Function Code Snippet



export default async function main(args) {
const { ZDAPIKey, body, subject, customerEmail, customerName } = args.inputVars; //This is where you map the input variables you are importing

//Check if the user has inputted the required variables
if (!ZDAPIKey || !customerEmail || !body ) {
return {
//Returns the error path so we can continue the design
next: { path: 'error' },
//Renders a debug message in Voiceflow
trace: [{ type: "debug", payload: { message: "Missing required input variables for this function" } }]
};
}

//This creates and stringifys the body for our fetch request
const data = JSON.stringify({
"ticket": {
"comment": {
"body": body
},
"subject": subject,
"requester": { "name": customerName, "email": customerEmail }
}
});

//This defines the URL for our fetch request
const url = 'https://example.zendesk.com/api/v2/tickets'

//This packages the rest of the information for the fetch request for an example API call to Zendesk
const config = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Basic ${ZDAPIKey}`, // Base64 encoded "username/token:apitoken"
},
body : data,
};


//This is where we made the fetch request, we use try-catch for error handling
try {

//Make the fetch request
const response = await fetch(url, config);

// Check if the response status is OK (status in the range 200-299)
if (!response.ok) {
// If not OK, throw an error to be caught by the catch block
throw new Error(`HTTP error! status: ${response.status}`);
}

//Map the fetch request response
const responseBody = response.json; //IMPORTANT: functions uses .json instead of .json() - see documentation for details

//Checks if the fetch request returned a body
if (!responseBody || typeof responseBody !== 'object') {
//If no body was returned, throw an error
throw new Error(`Invalid or missing response body from the API`);
}

//Map a value from the responseBody, we will use this as an output variable
const TicketID = responseBody.ticket.id

// Create the return objects if this is successfull
return {
//Map our output variables
outputVars: { ID: TicketID },
//Map the success path so we can continue in our flow
next: { path: 'success' },
//Render a text step that shows the ticket ID and displays a success emssage
trace: [{ type: "text", payload: { message: `Ticket Successfully Created with ID ${TicketID}` }}]
};
}

//Catches all the errors we threw and displays the debug message
catch (error) {
return {
//Maps the error path so we can continue in our design
next: { path: 'error' },
//Renders a debug message in Voiceflow with the error
trace: [{ type: "debug", payload: { message: "Error:" + error.message } }]
};
}
}

copy-icon

Explore More Templates

Build and submit a Template to have it featured in the community.

ghraphic
No items found.
No items found.