# Asterisk Project : Contexts, Extensions, and Priorities

Added by malcolmd , edited by lmadsen on Feb 24, 2011

The dialplan is organized into various sections, called contexts. Contexts are the basic organizational unit within the dialplan, and as such, they keep different sections of the dialplan independent from each other. We'll use contexts to enforce security boundaries between the various parts of our dialplan, as well as to provide different classes of service to groups of users.

The syntax for a context is exactly the same as any other section heading in the configuration files, as explained in Section 206.2.1. Sections and Settings. Simply place the context name in square brackets. For example, here is the context we defined in the previous module:

    [users]

Within each context, we can define one or more extensions. As explained in the previous module, an extension is simply a named set of actions. Asterisk will perform each action, in sequence, when that extension number is dialed. The syntax for an extension is:

    exten => number,priority,application([parameter[,parameter2...]])

As an example, let's review extension 6001 from the previous module. It looks like:

    exten => 6001,1,Dial(SIP/demo-alice,20)

In this case, the extension number is 6001, the priority number is 1, the application is Dial(), and the two parameters to the application are SIP/demo-alice and 20.
Within each extension, there must be one or more priorities. A priority is simply a sequence number. The first priority on an extension is executed first. When it finishes, the second priority is executed, and so forth.

Priority numbers
Priority numbers must begin with 1, and must increment sequentially. If Asterisk can't find the next priority number, it will terminate the call. We call this auto-fallthrough. Consider the example below:

    exten => 6123,1,do something
    exten => 6123,2,do something else
    exten => 6123,4,do something different

In this case, Asterisk would execute priorites one and two, but would then terminate the call, because it couldn't find priority number three.

Priority number can also be simplied by using the letter n in place of the priority numbers greater than one. The letter n stands for next, and when Asterisk sees priority n it replaces it in memory with the previous priority number plus one. Note that you must still explicitly declare priority number one.

    exten => 6123,1,do something
    exten => 6123,n,do something else
    exten => 6123,n,do something different

You can also assign a label (or alias) to a particular priority number by placing the label in parentheses directly after the priority number, as shown below. Labels make it easier to jump back to a particular location within the extension at a later time.

    exten => 6123,1,do something
    exten => 6123,n(repeat),do something else
    exten => 6123,n,do something different

Here, we've assigned a label named repeat to the second priority.

Included in the Asterisk 1.6.2 branch (and later) there is a way to avoid having to repeat the extension name/number or pattern using the same => prefix.

    exten => _1NXXNXXXXXX,1,do something
    same => n(repeat),do something else
    same => n,do something different