Skip to navigation

Connect a SIP trunk

Summary

This guide connects a workspace SIP setup, configures its connections and trunks, and imports an existing number. It also follows asynchronous operations and shows how to edit or remove the setup. It does not cover agent assignments; see Route phone calls.

Facts

ItemValue
Capabilitynumbers.assign
Setup operationsgetSipTrunkSetup, connectSipTrunk, disconnectSipTrunk, regenerateSipAddress
Setup pathsGET, POST, DELETE /telephony/sip-trunk; POST /telephony/sip-trunk/address/regenerate
Inbound operationscreateSipInboundConnection, updateSipInboundConnection, deleteSipInboundConnection
Inbound pathsPOST /telephony/sip-trunk/inbound-connections; PATCH, DELETE /telephony/sip-trunk/inbound-connections/{connection_id}
Outbound operationscreateSipOutboundTrunk, updateSipOutboundTrunk, deleteSipOutboundTrunk
Outbound pathsPOST /telephony/sip-trunk/outbound-trunks; PATCH, DELETE /telephony/sip-trunk/outbound-trunks/{trunk_id}
Number importimportSipNumber: POST /telephony/sip-trunk/phone-numbers/import
StreamstreamSipTrunkSetup: GET /telephony/sip-trunk/stream, text/event-stream
HeadersAuthorization on every request; Idempotency-Key on every mutation; If-Match on edits, deletes and address regeneration
Regionsindia, global; each entry of the setup’s addresses has a region and a region_state (open or closed)
CredentialsOutbound trunk username and password are write-only; reads expose has_password
Failure fieldsfailed_step and failure_reason on the setup; failure_reason on each operation. Both are null when nothing failed
failure_reasonprovider_refused, provider_conflict, provider_unavailable, provider_auth_failed, provider_rate_limited, sip_trunk_not_ready, internal_error. The list is open
EnvironmentRASEN_API_BASE (ending in /v1), RASEN_API_KEY, ETAG, CONNECTION_ID, TRUNK_ID, SIP_ENDPOINT, PHONE_NUMBER
Python3.10 or later, with httpx
TypeScriptNode.js 22 or later, with built-in fetch

Steps

Set SIP_ENDPOINT to your carrier or PBX host name, without a scheme or path. Set PHONE_NUMBER to an E.164 number you already own at your carrier. Use the returned resource_id for CONNECTION_ID after creating an inbound connection, and for TRUNK_ID after creating an outbound trunk. For an existing setup, read those IDs from inbound_connections and outbound_trunks.

1. Read the setup and its ETag

curl --fail-with-body -sS -i "$RASEN_API_BASE/telephony/sip-trunk" \
-H "Authorization: Bearer $RASEN_API_KEY"

Set ETAG to the returned ETag, including its quotes. Refresh ETAG from a mutation response or repeat this read before each later edit. If enabled is false, SIP trunking is unavailable for the workspace. A null address has not been created yet.

2. Connect or resume the setup

curl --fail-with-body -sS -i -X POST "$RASEN_API_BASE/telephony/sip-trunk" \
-H "Authorization: Bearer $RASEN_API_KEY" \
-H "Idempotency-Key: $(uuidgen)" \
-H "Content-Type: application/json" -d '{"label":"Support SIP"}'

The 202 response contains setup, resource_id and operation_id. Acceptance does not mean the asynchronous operation succeeded.

This request is also the retry for a failed setup. If the setup reports state: failed, read failed_step and failure_reason first, then send the request again with a new Idempotency-Key. If the same failure_reason comes back, contact support with the X-Request-Id of the retry.

3. Follow setup operations

curl --fail-with-body -N -sS "$RASEN_API_BASE/telephony/sip-trunk/stream" \
-H "Authorization: Bearer $RASEN_API_KEY" -H "Accept: text/event-stream"

Each sip.setup event contains a SipTrunkSnapshot: setup, up to 100 recent operations, and revision. Match operation_id to an operation’s id, then inspect its state. States include requested, dispatched, running, succeeded, failed and cancelled. A failed operation has a failure_reason, with the same codes as the setup’s failure_reason. A bind_number operation that failed with sip_trunk_not_ready ran before the setup was ready: wait for state: ready, then provision the number again.

Keep the last SSE id and send it as Last-Event-ID when reconnecting. This suppresses an unchanged snapshot; reconnecting returns the latest state rather than a historical event log. The stream never creates or retries work, and it checks API-key authorization again on every read. Treat unknown states as unknown rather than success.

4. Create an inbound connection

curl --fail-with-body -sS -i -X POST "$RASEN_API_BASE/telephony/sip-trunk/inbound-connections" \
-H "Authorization: Bearer $RASEN_API_KEY" -H "Idempotency-Key: $(uuidgen)" \
-H "Content-Type: application/json" -d '{"region":"global"}'

The answer is 201 with a SipTrunkMutation. Give your carrier the uri of the entry in the returned setup.addresses whose region matches the connection: the setup’s addresses are the SIP addresses your carrier points at. A null uri does not exist yet; read the setup again or follow the stream until it has a value. Use carrier_ips and enforce_carrier_ips to restrict which carrier IP addresses or CIDR ranges can send calls.

5. Create an outbound trunk

curl --fail-with-body -sS -i -X POST "$RASEN_API_BASE/telephony/sip-trunk/outbound-trunks" \
-H "Authorization: Bearer $RASEN_API_KEY" -H "Idempotency-Key: $(uuidgen)" \
-H "Content-Type: application/json" \
-d '{"kind":"carrier","label":"Support carrier","region":"global","endpoint":"'"$SIP_ENDPOINT"'"}'

The answer is 202. Follow the operation until the trunk is active. The default port is 5060 and the default transport is udp; set these to the values your carrier requires. Use kind: pbx for a PBX trunk. If your carrier requires credentials, supply username and password from your secret manager in the write body. Neither credential is returned by reads.

6. Import an existing SIP number

curl --fail-with-body -sS -i -X POST "$RASEN_API_BASE/telephony/sip-trunk/phone-numbers/import" \
-H "Authorization: Bearer $RASEN_API_KEY" -H "Idempotency-Key: $(uuidgen)" \
-H "Content-Type: application/json" \
-d '{"e164":"'"$PHONE_NUMBER"'","inbound_connection_id":"'"$CONNECTION_ID"'","outbound_trunk_id":"'"$TRUNK_ID"'"}'

The answer is 201. Import does not buy a number. Continue with Route phone calls to assign a published agent.

7. Edit an inbound connection

Use these maintenance steps when you need to change an existing setup. Read the current setup and refresh ETAG before each mutation that requires If-Match. This example restricts inbound calls to an illustrative carrier IP; replace it with your carrier’s documented addresses.

curl --fail-with-body -sS -i -X PATCH "$RASEN_API_BASE/telephony/sip-trunk/inbound-connections/$CONNECTION_ID" \
-H "Authorization: Bearer $RASEN_API_KEY" -H "Idempotency-Key: $(uuidgen)" -H "If-Match: $ETAG" \
-H "Content-Type: application/json" -d '{"carrier_ips":["192.0.2.10/32"],"enforce_carrier_ips":true}'

8. Edit an outbound trunk

curl --fail-with-body -sS -i -X PATCH "$RASEN_API_BASE/telephony/sip-trunk/outbound-trunks/$TRUNK_ID" \
-H "Authorization: Bearer $RASEN_API_KEY" -H "Idempotency-Key: $(uuidgen)" -H "If-Match: $ETAG" \
-H "Content-Type: application/json" -d '{"label":"Support carrier updated"}'

The answer is 202; follow any returned operation_id on the setup stream.

9. Regenerate the workspace SIP address

curl --fail-with-body -sS -i -X POST "$RASEN_API_BASE/telephony/sip-trunk/address/regenerate" \
-H "Authorization: Bearer $RASEN_API_KEY" -H "Idempotency-Key: $(uuidgen)" -H "If-Match: $ETAG"

The answer is 202. Follow the setup stream and use the resulting address for your carrier configuration.

10. Delete an unused inbound connection

Remove dependencies first if the API refuses deletion with in_use or number_bound.

curl --fail-with-body -sS -i -X DELETE "$RASEN_API_BASE/telephony/sip-trunk/inbound-connections/$CONNECTION_ID" \
-H "Authorization: Bearer $RASEN_API_KEY" -H "Idempotency-Key: $(uuidgen)" -H "If-Match: $ETAG"

11. Delete an unused outbound trunk

curl --fail-with-body -sS -i -X DELETE "$RASEN_API_BASE/telephony/sip-trunk/outbound-trunks/$TRUNK_ID" \
-H "Authorization: Bearer $RASEN_API_KEY" -H "Idempotency-Key: $(uuidgen)" -H "If-Match: $ETAG"

The answer is 202; follow its operation before treating the trunk as deleted.

12. Disconnect the setup

curl --fail-with-body -sS -i -X DELETE "$RASEN_API_BASE/telephony/sip-trunk" \
-H "Authorization: Bearer $RASEN_API_KEY" -H "Idempotency-Key: $(uuidgen)" -H "If-Match: $ETAG"

The answer is 202; inspect the operation’s result on the stream.

Rules

  • MUST send the same body and Idempotency-Key when retrying a lost response.
  • MUST read the current setup and send its ETag on operations that require If-Match.
  • MUST inspect the operation state after a 202 response.
  • MUST NOT put carrier credentials in logs or client-side applications.
  • MUST NOT treat an unfamiliar stream state as success.
  • MUST keep carrier number ownership and routing configuration consistent with your imported numbers.
  • MUST wait for state: ready before you provision a number on the SIP trunk.
  • MUST keep an unknown failure_reason or failed_step value, and treat it as a failure.

Verify

curl --fail-with-body -sS -i "$RASEN_API_BASE/telephony/sip-trunk" \
-H "Authorization: Bearer $RASEN_API_KEY"

After connection succeeds, expect state: ready and the addresses for available regions. If the setup reports state: failed, failed_step names the step that failed and failure_reason says why. A setup with state: ready, address_regenerating: true and a failed_step that starts with address_ has a stopped address regeneration, and the old address still routes. Retry it with regenerateSipAddress: connectSipTrunk answers 409 conflict while the setup is ready. After a maintenance operation, confirm its result in the setup and its terminal state in the stream.

Errors

CodeCauseFix
sip_trunk_not_enabledSIP trunking is not enabled for the workspace.Check getSipTrunkSetup and the workspace’s SIP availability.
carrier_not_activatedThe carrier named in details.carrier is not available to the workspace yet.Do not resend the unchanged request; contact support to activate the carrier.
ingress_unconfiguredThe ingress is not configured.Contact support; resending an unchanged request does not fix configuration.
conflict (stale_revision)The setup changed after your read.Read it again and reconcile before a new edit.
conflict (sip_trunk_connected)A setup is already connected.Read the existing setup before choosing another action.
conflict (sip_job_running)A SIP operation is running.Follow the operation on the stream before a new action.
conflict (in_use)The resource is in use.Reconcile its dependencies before deletion.
conflict (number_bound)A number is bound to the resource.Reconcile the number binding before deletion.
conflict (trunk_exists)The requested trunk already exists.Read the existing trunks and use or edit the matching trunk.
revision_requiredAn edit omitted If-Match.Read the setup and send its ETag.
telephony_not_connectedTelephony is not connected.Read the setup and connect it before repeating the intended action.
invalid_requestA field or body is invalid.Check the API reference and fix the indicated field.
provider_unavailable (details.reason: sip_trunk_not_ready)provisionPhoneNumber ran for a SIP number before the setup was ready. No carrier was contacted.Wait for state: ready (or retry a failed setup with connectSipTrunk), then send the request again with a new Idempotency-Key.
Setup failure_reason provider_refused, provider_conflict, provider_unavailable, provider_auth_failed or provider_rate_limitedThe SIP provider refused, could not be reached, refused the platform’s access or limited requests.Retry with connectSipTrunk, or with regenerateSipAddress when failed_step starts with address_. Contact support if it repeats.
Setup failure_reason internal_errorA platform fault stopped the setup.Retry with connectSipTrunk. Contact support if it repeats.