Skip to content

Asterisk Dialplan Tutorial: extensions.conf Explained

Learn the Asterisk dialplan in extensions.conf: contexts, extensions, priorities, pattern matching, special extensions, and how to check a call's match.

By · Updated · Published

Technician inspecting server racks with a handheld diagnostic tool
Photo by Valentin Lacoste · Unsplash License · via Unsplash

How does the Asterisk dialplan work?

The Asterisk dialplan is found in the extensions.conf file. It tells Asterisk what to do with each call. Asterisk finds the matching extension in the call's context and runs its priorities in order.

Each priority calls one application. This table lists the applications used in this guide.

ApplicationWhat it does
Answer()Answers the channel if it has not been answered.
Dial()Calls one or more channels. The timeout is the number of seconds to try.
Playback()Plays sound files, named without their file extension.
NoOp()Does nothing. The docs say it is useful for debugging.
Hangup()Hangs up the calling channel.
VoiceMail()Sends the caller to voicemail. Option u plays the unavailable greeting, and option b plays the busy greeting.

After you edit extensions.conf, run the CLI command dialplan reload to load the new dialplan. It loads changes without disrupting service. The rest of this guide explains how to write the file and how to check what it does.

What are contexts, extensions and priorities in extensions.conf?

Contexts are the basic organizational unit of the dialplan. A context is a named section heading such as [users]. It keeps parts of the dialplan independent of each other.

An extension line has this form: exten => number,priority,application(params). The docs show a line that dials a PJSIP endpoint for 20 seconds. This example uses extension 12 for the same idea:

INI config
[users]
exten => 12,1,Dial(PJSIP/demo-alice,20)

In this line, 12 is the extension, 1 is the priority and Dial() is the application. The 20 is the timeout in seconds.

Priorities start at 1, and you must still write priority 1 yourself. After that, n means next: the previous priority plus one. Since Asterisk 1.6.2 and later, same => repeats the previous extension name, so you do not write the number again.

INI config
[users]
exten => 12,1,Answer()
 same => n,Playback(welcome)
 same => n,Dial(PJSIP/demo-alice,20)
 same => n,Hangup()

Read those four lines as one extension. The first line sets extension 12 and priority 1. Each same => n line adds the next priority, so the call runs Answer(), then Playback(), then Dial(), then Hangup().

Playback(welcome) plays a sound file named welcome, with no extension. If the priorities skip a number, the call stops at the gap. With priorities 1, 2 and 4, Asterisk runs 1 and 2, then ends the call. You can also give a priority a label with n(label), so another line can jump to it.

What does a first working extensions.conf look like?

Laptop with code, headphones, phone, and mouse on desk
Photo by Daniil Komov · Unsplash License · via Unsplash

This example uses sample values only: extension 12 and PJSIP/demo-alice are not real endpoints.

INI config
[globals]
MYGLOBALVAR=somevalue

[users]
exten => 12,1,NoOp(Start of the call to 12)
 same => n,Answer()
 same => n,Playback(welcome)
 same => n,Dial(PJSIP/demo-alice,20)
 same => n,VoiceMail(12,u)
 same => n,Hangup()

The [globals] section declares the global variable MYGLOBALVAR. The [users] heading starts a context. NoOp() comes first. It does nothing, and the docs say it is useful for debugging. Answer() answers the call, and Playback() plays the welcome file. Dial() dials the PJSIP endpoint for 20 seconds.

VoiceMail(12,u) sends the caller to voicemail for mailbox 12 and plays the unavailable greeting, because of option u. Hangup() ends the call.

After you save the file, run these commands at the Asterisk CLI:

Output
*CLI> dialplan reload
*CLI> dialplan show 12@users

The dialplan show line tells you which extension Asterisk will use when a call to 12 arrives in the users context. Keep the example small while you test it. Add one line at a time, reload, and check with dialplan show before you add the next.

How does Asterisk choose between matching patterns?

Flow chart of pattern sort order: extensions without a pattern come first in ASCII order, then patterns with N before X, then the period after sets and ! after the period. The first match wins.
Pattern sort order: which extension wins

Patterns start with an underscore. Asterisk sorts the extensions in a context and uses the first one that matches. The pattern matching reference defines these characters:

CharacterMatches
XOne digit, 0-9
ZOne digit, 1-9
NOne digit, 2-9
[1237-9]One character from the set: 1, 2, 3, 7, 8 or 9
.One or more remaining characters, for example _9876.
!Zero or more remaining characters, used for overlap dialing
[.] or [!]A literal period or exclamation mark

The sort order works like this:

  1. Extensions without a pattern come first, in ASCII order.
  2. Then patterns, with the most constrained first for each digit. N comes before X.
  3. A period comes after sets, and ! comes after the period.
  4. The first match in the sorted list wins.

Wildcards match any character, not only digits, so a pattern can match more than you expect.

A pattern is useful when one line should handle a group of numbers. Use a plain extension when you want one exact number. Exact extensions sort first, so an exact line wins over a pattern that also matches the same number.

The period is the wildcard to watch. A pattern such as _9876. needs at least one more character after the fixed digits, so it does not match 9876 on its own. Test each pattern with dialplan show before you rely on it.

What does our compiled dialplan reference cover?

We compiled this reference from the Asterisk dialplan documentation on November 14, 2026. Nothing in it was tested. Each row comes from a page in that documentation.

The reference puts these topics side by side: file and reload, structure, pattern characters and sort rules, special extensions, includes, and variables. Many tutorials spread these topics across many pages. This guide keeps them together so you can read them in one pass.

What do the s, i, t and h extensions do?

The s extension starts analog calls and macros, i handles invalid digits, t handles timeouts, and h runs when the call hangs up.

ExtensionRuns when
sA call starts, for analog calls and macros. It is not a catch-all.
iDigits dialed during Background() or WaitExten() match no extension.
tThe caller waits too long to respond to Background() or WaitExten().
hThe call hangs up. It runs in the current context and is useful for clean-up.

The special extensions page also describes e, which catches a missing i, t or T, and T, an absolute timeout.

Many top tutorials leave out the h extension. Put clean-up steps in h instead, because it runs when the call hangs up. The s extension is not a catch-all, so do not rely on it to catch every number.

How do include, variables and expressions work?

An include => line followed by a context name makes Asterisk also search that context. Inside one context, Asterisk tries explicit extensions first, then pattern matches, then includes (depth-first), then switches. Watch for overlapping patterns across included contexts: the docs give an example where an included _. catches priority 3.

INI config
[users]
include => features
exten => 12,1,Dial(PJSIP/demo-alice,20)
 same => n,Hangup()

Here a number that matches nothing in [users] is also looked up in the features context. The context name features is an example value. To avoid surprises, keep broad patterns such as _. out of any context that other contexts include.

Asterisk uses channel variables and global variables, and their names are case-sensitive. Each call gets the built-in CONTEXT, EXTEN and PRIORITY variables, which hold its current context, extension and priority.

Global variables can be declared in the [globals] section, as shown above. You can also set one from the dialplan with Set(GLOBAL(MYGLOBALVAR)=somevalue). SayNumber(digits) plays the sounds for the given digits, so SayNumber(${EXTEN}) reads the current extension aloud.

A substring takes part of a variable. With NUMBER=98765, the results are:

ExpressionResult
${NUMBER:2}765
${NUMBER:-2}65
${NUMBER:0:3}987
${NUMBER:1:3}876

${NUMBER:2} removes the first two digits and returns 765. A negative number counts from the end of the value.

Anything inside $[ ] is evaluated as an expression. For example, Set(lala=$[1 + 2]) stores the value 3 as lala.

How do I reload the dialplan and check why a call does not match?

Flow chart of the steps: edit extensions.conf, run dialplan reload at the Asterisk CLI, then run dialplan show 6421@users to see which extension Asterisk will use.
Reload and check a dialplan change

Reload after every change, then ask Asterisk which extension it will run for a number.

  1. Edit extensions.conf.
  2. At the Asterisk CLI, run dialplan reload.
  3. Run dialplan show 6421@users to see which extension Asterisk will use when a call to 6421 arrives in the users context.
Output
*CLI> dialplan reload
*CLI> dialplan show 6421@users

If the result is not the extension you expected, check the sort order rules above. A pattern that uses . or ! can match more than you intend.

Check three things in order: the context the call arrives in, the extension or pattern that should match, and the priority numbers. A gap in the priorities ends the call at the gap, so a missing line can explain a call that stops early. If the number matches nothing in the context, check the contexts it includes, because Asterisk searches those next.

How do menus and subroutines work together?

These two examples use only the applications and extension types covered above. The file names welcome and invalid are example values.

The first example is a small menu. BackGround() plays the welcome file while it waits for digits, and WaitExten() waits for the caller to enter an extension.

INI config
[ivr]
exten => s,1,Answer()
 same => n(menu),BackGround(welcome)
 same => n,WaitExten()

exten => 1,1,Dial(PJSIP/demo-alice,20)
 same => n,Hangup()

exten => i,1,Playback(invalid)
 same => n,Goto(s,menu)

exten => t,1,Hangup()

If the caller presses 1, the call goes to extension 1. If the digits match no extension, the i extension plays the invalid file and jumps back to the menu label with Goto(). If the caller is too slow, the t extension hangs up.

The second example is a subroutine. Gosub() jumps to a label and saves the return address, and Return() ends the subroutine. Gosub() is the choice this guide uses for reusable code, because the docs describe it for subroutines.

INI config
[subs]
exten => s,1,NoOp(Greeting subroutine)
 same => n,Playback(welcome)
 same => n,Return()

[users]
exten => 12,1,Gosub(subs,s,1)
 same => n,Dial(PJSIP/demo-alice,20)
 same => n,Hangup()

Extension 12 runs the greeting in the subs context, returns to the line after Gosub(), and then dials. Check both contexts with dialplan show users and dialplan show subs, as in the steps above.

More guides in Open-Source PBX and SIP Servers.

Frequently asked questions

How does include work and in what order are contexts searched?
An include => line followed by a context name makes Asterisk also search that context. Inside one context, Asterisk tries explicit extensions first, then pattern matches, then includes depth-first, and then switches.
Should I use Macro() or Gosub() for reusable dialplan code?
This guide uses Gosub() and Return(), because the Asterisk docs describe them for reusable subroutines. It does not compare them with Macro().

Sources

  1. The Asterisk dialplan is stored in the extensions.conf file. (docs.asterisk.org)
  2. Answer([delay,[options]]) answers the channel if it has not been answered. (docs.asterisk.org)
  3. Dial(Technology/Resource&[Technology2/Resource2[&...]],[timeout,[options,[URL]]]) calls on (docs.asterisk.org)
  4. Playback(filename&[filename2[&...]],[options]) plays sound files, named without their file (docs.asterisk.org)
  5. NoOp([text]) does nothing; it is useful as a first priority or a marker. (docs.asterisk.org)
  6. Hangup([causecode]) hangs up the calling channel. (docs.asterisk.org)
  7. VoiceMail(mailbox1&[mailbox2[&...]],[options]) sends the caller to voicemail; option u pla (docs.asterisk.org)
  8. Contexts are the basic organizational unit of the dialplan. (docs.asterisk.org)
  9. Pattern-match extensions always begin with an underscore. (docs.asterisk.org)
  10. If digits dialed during Background() or WaitExten() match no extension, the call goes to t (docs.asterisk.org)
  11. Asterisk creates the channel variables CONTEXT, EXTEN and PRIORITY holding the current con (docs.asterisk.org)
  12. Anything inside $[ ] is evaluated as an expression, e.g. Set(lala=$[1 + 2]). (docs.asterisk.org)