Release Phone Numbers
Releases (deletes) one or more phone numbers from the Telnyx account and marks them as released in the agency_phone_numbers database table. This is a destructive, permanent action -- released numbers cannot be recovered. Requires explicit confirmation.
Endpoint
| Property | Value |
|---|---|
| Function Name | phone-numbers-release |
| HTTP Method | POST |
| Authentication | Supabase JWT (Bearer token) |
| Required Role | agency_owner only |
Request
Headers
Authorization: Bearer <supabase_jwt_token>
Content-Type: application/json
Body
| Field | Type | Required | Description |
|---|---|---|---|
phone_number_ids | array of string (UUIDs) | Yes | Array of phone number record IDs to release |
confirm | boolean | Yes | Must be true to confirm the destructive action |
Example:
{
"phone_number_ids": [
"a1b2c3d4-e5f6-7890-abcd-ef1234567890",
"b2c3d4e5-f6a7-8901-bcde-f12345678901"
],
"confirm": true
}
Response
Full Success (200)
{
"success": true,
"released": [
{
"id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
"phone_number": "+19705551234"
},
{
"id": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
"phone_number": "+19705555678"
}
],
"failed": [],
"summary": {
"total_requested": 2,
"released_count": 2,
"failed_count": 0
},
"message": "Successfully released 2 phone number(s)"
}
Partial Success (207)
Returned when some numbers were released successfully and others failed.
{
"success": false,
"released": [
{
"id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
"phone_number": "+19705551234"
}
],
"failed": [
{
"id": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
"phone_number": "+19705555678",
"error": "Telnyx error: 422"
}
],
"summary": {
"total_requested": 2,
"released_count": 1,
"failed_count": 1
},
"message": "Released 1 number(s), 1 failed"
}
Numbers that were already in released status are included in the released array with a note: "Already released" field.
If a Telnyx deletion succeeds but the subsequent database update fails, the failed entry will include "telnyx_released": true to indicate the number was removed from Telnyx but the database record was not updated.
Error Responses
| Status | Condition |
|---|---|
| 400 | phone_number_ids array is missing or empty |
| 400 | confirm is not set to true |
| 400 | Telnyx API key is not configured for the agency |
| 401 | Missing or invalid authorization header |
| 403 | Authenticated user does not belong to an agency |
| 403 | User role is not agency_owner (only owners can release numbers) |
| 404 | None of the provided phone number IDs belong to the user's agency |
| 405 | HTTP method is not POST |
| 500 | Database query to fetch phone numbers failed |
| 500 | All phone numbers failed to release |
| 500 | Unexpected server error |
Behavior
- Authenticates the user via the Supabase JWT extracted from the
Authorizationheader. - Looks up the user's
agency_idandrolefrom theuserstable. - Enforces that only users with the
agency_ownerrole can release numbers. This is stricter than other phone number operations because releasing is a destructive action. - Retrieves the agency's decrypted Telnyx API key by calling
get_agency_credentials. - Validates that
phone_number_idsis a non-empty array and thatconfirmis exactlytrue. - Fetches the matching phone number records from
agency_phone_numbersscoped to the agency. - Processes each phone number sequentially:
- Already released: If the number's status is already
released, it is added to thereleasedresults with a note and skipped. - Telnyx deletion: Calls the Telnyx
DELETE /v2/phone_numbers/{id}endpoint using thephone_number_idif available, or falls back to the phone number string. A 404 response from Telnyx is treated as acceptable (the number may no longer exist on Telnyx's side). - Database update: Updates the
agency_phone_numbersrecord, settingstatustoreleased,is_activetofalse,agent_mapping_idanddirectiontonull, andreleased_atto the current timestamp. - If either the Telnyx deletion or database update fails, the number is added to the
failedarray with an error description.
- Already released: If the number's status is already
- Returns the overall result:
- HTTP 200 if all numbers were released successfully.
- HTTP 207 (Multi-Status) if some succeeded and some failed.
- HTTP 500 if all numbers failed to release.
Database Tables Read
users-- to resolve the caller'sagency_idandroleagency_phone_numbers-- to fetch the numbers to be released
Database Tables Written
agency_phone_numbers-- updates each released number'sstatus,is_active,agent_mapping_id,direction, andreleased_at
Database Functions Called
get_agency_credentials-- to retrieve the decrypted Telnyx API key
External APIs Called
- Telnyx
DELETE /v2/phone_numbers/{phone_number_id}-- deletes the phone number from the Telnyx account
Related Functions
- Search Available Phone Numbers -- search for new numbers to replace released ones
- Purchase Phone Numbers -- purchase replacement numbers
- List Agency Phone Numbers -- view all numbers including released ones
- Assign Phone Numbers -- manage assignments before releasing