extended-salmon
extended-salmon3w ago

Can we create a workflow object and execute it directly in our code?

I’ve been experimenting with the Web SDK. Right now, I’m calling vapi.start with a workflowId that I created beforehand in the dashboard. However, I’d like to avoid creating a new workflow in the web app, I would like to manage this in the code. Is it possible to define a workflow object directly in my code and pass it to vapi.start (instead of passing a workflowId)? Or do workflows always need to be created via the API/dashboard first? I would like to define the workflow programmatically and then execute it to manage for example the user prefered language, etc....
1 Reply
Shubham Bajaj
Shubham Bajaj2w ago
No, you don't have to create a workflow via API/dashboard before using the web SDK. Instead, what you are looking for is using transient workflow configurations. These can be created via API or maintained in your code and because they are transient, only exist when the call is initialized. I will provide an example snippet below:
import { useVapiCall } from '@/hooks/useVapiCall';

const { startCallWithWorkflow } = useVapiCall();

// Define your workflow structure
const transientWorkflow = {
name: 'My Transient Workflow',
nodes: [
{
name: 'introduction',
type: 'conversation',
isStart: true,
prompt: 'You are a helpful assistant',
messagePlan: {
firstMessage: 'Hello! How can I help you today?',
},
metadata: {
position: { x: 0, y: 0 }
}
},
{
name: 'end',
type: 'end',
metadata: {
position: { x: 200, y: 100 }
}
}
],
edges: [
{
from: 'introduction',
to: 'end'
}
],
globalPrompt: 'Be helpful and concise',
model: {
provider: 'openai',
model: 'gpt-4'
},
voice: {
provider: 'azure',
voiceId: 'andrew'
}
};

// Start call with transient workflow
startCallWithWorkflow(transientWorkflow, {
// Optional variable values
customerName: 'John',
customVariable: 'value'
});
import { useVapiCall } from '@/hooks/useVapiCall';

const { startCallWithWorkflow } = useVapiCall();

// Define your workflow structure
const transientWorkflow = {
name: 'My Transient Workflow',
nodes: [
{
name: 'introduction',
type: 'conversation',
isStart: true,
prompt: 'You are a helpful assistant',
messagePlan: {
firstMessage: 'Hello! How can I help you today?',
},
metadata: {
position: { x: 0, y: 0 }
}
},
{
name: 'end',
type: 'end',
metadata: {
position: { x: 200, y: 100 }
}
}
],
edges: [
{
from: 'introduction',
to: 'end'
}
],
globalPrompt: 'Be helpful and concise',
model: {
provider: 'openai',
model: 'gpt-4'
},
voice: {
provider: 'azure',
voiceId: 'andrew'
}
};

// Start call with transient workflow
startCallWithWorkflow(transientWorkflow, {
// Optional variable values
customerName: 'John',
customVariable: 'value'
});

Did you find this page helpful?