Asterisk ARI call lifecycle and cleanup
Model a call as resources and state changes, not a list of HTTP requests that must all succeed in order. A caller can hang up between two application operations, and an external service can respond after the channel it was meant to affect has gone away.
Source-reviewed, 20 October 2026. Prepared from the cited documentation. No ARI call lifecycle has been independently tested by Phone Guides. Scope: Asterisk 22 conventional ARI application design; use branch-specific resource and event schemas.
Record ownership explicitly
For each application operation, identify the channel or bridge, which application owns its handling, its expected state and what ends that ownership. Keep identifiers separate from caller-display values and subscriber information.
Use the upstream ARI channels explanation and bridges explanation to understand the resources. The branch-22 ARI reference defines exact requests and event models.
Plan the normal and interrupted paths
| Stage | Normal question | Interrupted-path question |
|---|---|---|
| Stasis entry | Did the intended application receive the channel? | What if the application is unavailable? |
| External lookup | Which result selects the next operation? | What if the caller hangs up or the lookup times out? |
| Destination creation | Which operation and resource identify the called leg? | Did a failed connection leave an already-created resource? |
| Bridge/media work | Which resources must be connected or active? | What if a participant leaves first? |
| Completion | Which resources and application records are released? | Can cleanup run again safely? |
The Stasis setup guide supplies the entry boundary. A channel being present in Asterisk does not imply it is available for arbitrary ARI control under your application.
Tie requests to a live resource
For an application-owned incoming channel, record the identifier from the actual Stasis event. Do not derive it from the displayed caller number. The branch-22 channels API defines the following operations. Paths below include the usual /ari base and show templates, not commands executed against a PBX:
GET /ari/channels/{channelId}
POST /ari/channels/{channelId}/answer
DELETE /ari/channels/{channelId}
Substitute and URL-encode the specific resource identifier through the HTTP client's parameter/path handling. Keep authentication server-side on the established protected connection; do not put a credential in the URL. Getting a resource describes its state at that moment. It does not reserve the channel against hangup before the next request.
| Application state | Permitted next decision | If the channel ends first |
|---|---|---|
| Stasis entry received | Validate this call's task and permissions | Stop pending work for that call |
| Caller treatment selected | Answer only if this application intends to answer | Treat the absent channel as ended |
| Media/bridge setup pending | Act only on resources the application owns | Release any now-unused owned resources |
| Conversation ending | Hang up or return to the intended dialplan path | Repeated cleanup must remain harmless |
This table is an application design, not a guaranteed ordering of every ARI event. A missing resource and a resource outside Stasis can require different responses; inspect the specific operation's documented error result. Do not retry every non-success response as another origination.
For bridging, the branch-22 bridges API defines bridge creation, membership and deletion separately. Record which bridge your application created and which channels it placed there. Removing a channel from a bridge, hanging up that channel and destroying a bridge are separate operations; choose the intended end state explicitly.
Handle races and absent resources
Define how a handler behaves when its target has already ended. Use the resource response and current state rather than treating every missing-resource result as an incident requiring another call attempt. Keep cleanup tolerant of an operation that another event handler already completed.
Do not claim a particular total event order unless the relevant protocol/reference establishes it for that path. Record which event or response permits each transition in your own state model. Separate business completion from the final release of call resources.
Retry with reconciliation
When a request's result is uncertain, determine whether it could already have changed Asterisk before sending it again. Resource-creating actions and external billing/record writes need explicit duplicate handling. A successful reconnect does not by itself resolve that uncertainty.
With Asterisk 22.5.0 and later, an outbound ARI WebSocket can reconnect to an external application server; for a per-call connection that exhausts its configured reconnect attempts, Stasis() returns to the dialplan with STASISSTATUS=FAILED. This is connection-level recovery, not evidence that missed events were replayed or that the application's channel and bridge state has moved to another Asterisk server. After reconnect, query the resources needed to reconcile the application's current responsibilities. Decide how abandoned application-owned work is detected and handled. See Asterisk resilience and recovery design for the distinction between ARI application redundancy and PBX call survival.
Reader checks
On your own application, exercise caller hangup before answer, hangup during a slow lookup, a missing resource, an interrupted request and a lost event connection. Compare active resources and application state afterwards. Check that cleanup does not affect another application's call.
Keep application version, API branch, state-transition notes and redacted event evidence together. Use monitoring to detect stuck work, and AGI/AMI/ARI selection if the task can use a smaller control surface.
Return to Asterisk documentation.