# Configure pre-agent workflow with Studio

Flex uses Twilio Studio to implement pre-agent workflows. Refer to the [Core Concepts: Studio](/docs/flex/admin-guide/core-concepts/studio-flows-functions-assets-twiml) chapter for a general introduction.

## Triggers

The Studio Flow is executed when one of the Trigger events happens. For Flex, the most relevant triggers are **Incoming Message**, **Incoming Call** and **Incoming Conversation**.

![Trigger options for incoming message, call, conversation, REST API, and subflow in Twilio Studio.](https://docs-resources.prod.twilio.com/c2f0d70c966fc3abf9452f925869195b0ddc811adef2cd39ce880e7a5be6166f.png)

To configure a Phone Number to trigger your desired Studio Flow, head to Phone Numbers > Manage > [Active numbers](https://www.twilio.com/console/phone-numbers/incoming?target-region=us1), choose the respective number, and set the Voice or Messaging trigger to call the respective Studio Flow.

## Use the Unified Profiles Studio integration for intelligent routing

You can use the [Search for a Profile Studio widget](/docs/studio/widget-library/search-for-a-profile) for personalized pre-agent handling and routing. The Search for a Profile widget uses [Unified Profiles capabilities](/docs/flex/admin-guide/setup/unified-profiles) to provide you with real-time profile data for each customer that calls or messages your business.

With the Search for a Profile widget, you can look up customer profiles in Twilio Segment by identifier attribute, such as phone number or email address. If a profile is found, the widget returns the profile SID and all the mapped traits for that profile, including custom and computed traits.

This contextual data provides a better understanding of each customer, which enables Flex to use your preferred pre-agent handling or direct the customer to the most relevant TaskRouter workflow for personalized routing.

![Unified Profiles flowchart for intelligent routing with customer profile and VIP split.](https://docs-resources.prod.twilio.com/f50397e6e8debd08e554a6f1a65dedefd88fd9c5d26754e05fec8c8a638f5402.png)

Intelligent routing enables use cases that include:

* Personalizing virtual agent interactions with real-time customer data for deflection or contextual handoff to an agent
* Intelligently routing customers to the correct queue based on their preferred language
* Personalizing self-service experiences that reduce escalations to human agents
* Using dynamic routing and IVR menus based on computed and predictive traits
* Using a specific profile trait for VIP customers and routing them to a VIP queue each time they call in

## Send to Flex widget

The [Send to Flex widget](/docs/studio/widget-library/send-flex) will create a TaskRouter Task for the incoming communication, and execute a TaskRouter Workflow to route it to an agent.

For the Incoming Call trigger, **Send to Flex** will enqueue the Voice and the caller will hear hold music while waiting for an agent to respond.

In Flex Conversations (which requires Flex UI 2.0), a new Trigger **Incoming Conversation** has been added. In this case, **Send to Flex** will create a new Interaction using the [/Interactions endpoint](/docs/proxy/api/interaction#create-an-interaction-resource), which then creates a new Task for assignment to a [Worker](/docs/flex/admin-guide/core-concepts/routing#workers).

Optionally, you can declare Task attributes, priority, timeout, and a hold music TwiML URL. To learn more, see [Send to Flex](/docs/studio/widget-library/send-flex).

## Building a pre-agent workflow

Use Studio Flows to build out any logic that needs to execute before a Task is created and routed to an agent. Typical use cases include IVR, chat bots, and call deflection. Some useful resources include:

* [Twilio Studio widget library documentation](/docs/studio/widget-library)
* Tutorial [Build an IVR for Flex with Twilio Studio](/docs/flex/admin-guide/tutorials/ivr) for a comprehensive example

## Studio REST API v2

The **Studio REST API v2** allows you to programmatically create, publish, and manage your Studio Flows without having to log in to the Twilio Console. Studio Flows are defined using a [JSON schema](/docs/studio/rest-api/v2/flow-definition) to configure the widgets and the functionality. The REST API also provides methods to trigger an outbound call or message, as well as for reporting on Executions that have completed.
