# Queue Calls

## Overview \[#overview]

This guide will explain how to use Twilio's [Queue feature](https://www.twilio.com/en-us/voice/features) to create a call queueing system. A Queue stores incoming calls in order. You can then connect the first call in the queue to another call easily. The complete code sample will show you how to accept an incoming call, place it into a queue and then connect a live agent to the first call in the queue.

For this demonstration we'll be using two Twilio phone numbers, so if you'd like to try the code out live, you'll need to buy two [phone numbers](https://www.twilio.com/user/account/voice/phone-numbers). For more help getting setup with Voice be sure to read our [Getting Started guide](/docs/voice/quickstart).

## Putting the Caller in a Queue \[#caller]

When our first number is called, Twilio will make a request to our server to place the *caller* in the Queue.

Enqueue example

```js
const VoiceResponse = require('twilio').twiml.VoiceResponse;


const response = new VoiceResponse();
response.enqueue({
    waitUrl: 'wait-music.xml'
}, 'support');

console.log(response.toString());
```

```py
from twilio.twiml.voice_response import Enqueue, VoiceResponse

response = VoiceResponse()
response.enqueue('support', wait_url='wait-music.xml')

print(response)
```

```cs
using System;
using Twilio.TwiML;
using Twilio.TwiML.Voice;


class Example
{
    static void Main()
    {
        var response = new VoiceResponse();
        response.Enqueue("support", waitUrl: new Uri("wait-music.xml", UriKind
            .Relative));

        Console.WriteLine(response.ToString());
    }
}
```

```java
import com.twilio.twiml.voice.Enqueue;
import com.twilio.twiml.VoiceResponse;
import com.twilio.twiml.TwiMLException;


public class Example {
    public static void main(String[] args) {
        Enqueue enqueue = new Enqueue.Builder("support")
            .waitUrl("wait-music.xml").build();
        VoiceResponse response = new VoiceResponse.Builder().enqueue(enqueue)
            .build();

        try {
            System.out.println(response.toXml());
        } catch (TwiMLException e) {
            e.printStackTrace();
        }
    }
}
```

```go
package main

import (
	"fmt"

	"github.com/twilio/twilio-go/twiml"
)

func main() {
	twiml, _ := twiml.Voice([]twiml.Element{
		&twiml.VoiceEnqueue{
			Name:    "support",
			WaitUrl: "wait-music.xml",
		},
	})

	fmt.Print(twiml)
}
```

```php
<?php
require_once './vendor/autoload.php';
use Twilio\TwiML\VoiceResponse;

$response = new VoiceResponse();
$response->enqueue('support', ['waitUrl' => 'wait-music.xml']);

echo $response;
```

```rb
require 'twilio-ruby'

response = Twilio::TwiML::VoiceResponse.new
response.enqueue(wait_url: 'wait-music.xml', name: 'support')

puts response
```

```xml
<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Enqueue waitUrl="wait-music.xml">support</Enqueue>
</Response>
```

We start by creating a [TwiML](/docs/voice/twiml) response that uses the [Enqueue](/docs/voice/twiml/enqueue) verb to create a new Queue and place the caller into it.

You can learn more about how to serve TwiML from your own application in our
[webhook guides](/docs). You can explore all the available
TwiML functionality [in our TwiML reference docs](/docs/voice/twiml).

## Connecting the Agent to the Queue \[#agent]

When our second number is called, Twilio will make a request to our server to connect the agent to the Queue.

Dialing a queue

```js
const VoiceResponse = require('twilio').twiml.VoiceResponse;


const response = new VoiceResponse();
const dial = response.dial();
dial.queue({
    url: 'about_to_connect.xml'
}, 'support');

console.log(response.toString());
```

```py
from twilio.twiml.voice_response import Dial, Queue, VoiceResponse

response = VoiceResponse()
dial = Dial()
dial.queue('support', url='about_to_connect.xml')
response.append(dial)

print(response)
```

```cs
using System;
using Twilio.TwiML;
using Twilio.TwiML.Voice;


class Example
{
    static void Main()
    {
        var response = new VoiceResponse();
        var dial = new Dial();
        dial.Queue("support", url: new Uri("about_to_connect.xml", UriKind
            .Relative));
        response.Append(dial);

        Console.WriteLine(response.ToString());
    }
}
```

```java
import com.twilio.twiml.voice.Dial;
import com.twilio.twiml.voice.Queue;
import com.twilio.twiml.VoiceResponse;
import com.twilio.twiml.TwiMLException;


public class Example {
    public static void main(String[] args) {
        Queue queue = new Queue.Builder("support").url("about_to_connect.xml")
            .build();
        Dial dial = new Dial.Builder().queue(queue).build();
        VoiceResponse response = new VoiceResponse.Builder().dial(dial).build();

        try {
            System.out.println(response.toXml());
        } catch (TwiMLException e) {
            e.printStackTrace();
        }
    }
}
```

```go
package main

import (
	"fmt"

	"github.com/twilio/twilio-go/twiml"
)

func main() {
	twiml, _ := twiml.Voice([]twiml.Element{
		&twiml.VoiceDial{
			InnerElements: []twiml.Element{
				&twiml.VoiceQueue{
					Url:  "about_to_connect.xml",
					Name: "support",
				},
			},
		},
	})

	fmt.Print(twiml)
}
```

```php
<?php
require_once './vendor/autoload.php';
use Twilio\TwiML\VoiceResponse;

$response = new VoiceResponse();
$dial = $response->dial('');
$dial->queue('support', ['url' => 'about_to_connect.xml']);

echo $response;
```

```rb
require 'twilio-ruby'

response = Twilio::TwiML::VoiceResponse.new
response.dial do |dial|
  dial.queue('support', url: 'about_to_connect.xml')
end

puts response
```

```xml
<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Dial>
        <Queue url="about_to_connect.xml">support</Queue>
    </Dial>
</Response>
```

To connect to the first caller in the *Queue* all we do is [Dial](/docs/voice/twiml/dial) the [*Queue*](/docs/voice/twiml/queue) by name.

## Automatically Connecting to the next Caller \[#redirect]

By default the call will end when the Agent or Caller hang up, but in most cases we'll want to connect to the next Caller automatically.

Automatically Connect to the Next Caller

```js
// Download the Node helper library from twilio.com/docs/node/install
const VoiceResponse = require('twilio').twiml.VoiceResponse;
const twiml = new VoiceResponse();

twiml.say('You will now be connected to the first caller in the queue.');
const dial = twiml.dial();
dial.queue('Queue Demo');
twiml.redirect();

console.log(twiml.toString());
```

```py
# Download the Python helper library from twilio.com/docs/python/install
from twilio.twiml.voice_response import VoiceResponse, Dial

r = VoiceResponse()
r.say("You will now be connected to the first caller in the queue.")
d = Dial()
d.queue("Queue Demo")
r.append(d)
r.redirect('http://example.org')
print(str(r))
```

```java
// Install the Java helper library from twilio.com/docs/java/install
import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.twilio.twiml.*;

public class Example extends HttpServlet {
  public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
    Say say =
        new Say.Builder("You will now be connected to the first caller in the queue.").build();

    Queue queueInDial = new Queue.Builder("Queue Demo").build();

    Dial dial = new Dial.Builder()
        .queue(queueInDial)
        .build();

    Redirect redirect = new Redirect.Builder().build();

    VoiceResponse twiml = new VoiceResponse.Builder()
        .say(say)
        .dial(dial)
        .redirect(redirect)
        .build();

    response.setContentType("application/xml");

    try {
      response.getWriter().print(twiml.toXml());
    } catch (TwiMLException e) {
      e.printStackTrace();
    }
  }
}
```

```php
<?php
// NOTE: This example uses the next generation Twilio helper library - for more
// information on how to download and install this version, visit
// https://www.twilio.com/docs/libraries/php
require_once '/path/to/vendor/autoload.php';

use Twilio\TwiML\VoiceResponse;

$response = new VoiceResponse();

$response->say("You will now be connected to the first caller in the queue.");
$dial = $response->dial();
$dial->queue("Queue Demo");
$response->redirect();

echo $response;
```

```rb
require 'sinatra'
require 'twilio-ruby'

# Handles the POST request from Twilio and generates the TwiML
# that connects agent with the first caller in the Queue
post '/agent/?' do
  response = Twilio::TwiML::Response.new do |r|
    r.Say 'You will now be connected to the first caller in the queue.'
    r.Dial do |d|
      d.Queue 'Queue Demo'
    end
    r.Redirect
  end
  response.text
end
```

```xml
<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Say>You will now be connected to the first caller in the queue.</Say>
  <Dial>
    <Queue>Queue Demo</Queue>
  </Dial>
  <Redirect></Redirect>
</Response>
```

An empty [`<Redirect>`](/docs/voice/twiml/redirect) verb will redirect the
caller to the beginning of the TwiML, which will then Dial and connect to the
next person in the Queue. As a final touch we add a [`<Say>`](/docs/voice/twiml/say) verb to tell the agent
that they are being connected to another caller.

## More sophisticated workflows \[#taskrouter]

With Twilio's [TaskRouter](/docs/taskrouter) you can implement very
sophisticated call-center workflows like:

* assigning available Agents to the next caller in the Queue
* offering tiered support plans, that allow premium customers to skip the queue
* routing certain callers to different teams based on business logic

Hopefully this guide got you up and running with Twilio Queues.
[Email us](/help/contact) and let us know what features of Queue you would like
to know more about.
