Skip to main content

Phone Number Tables

agency_phone_numbers

Purchased Telnyx phone numbers assigned to agencies. Each number can optionally be assigned to an agent mapping.

ColumnTypeDefaultNullableDescription
iduuiduuid_generate_v4()noPrimary key
agency_iduuidnoFK → agencies.id
phone_numbervarchar(20)noE.164 phone number (unique)
phone_number_idvarchar(255)yesTelnyx phone number resource ID
friendly_namevarchar(255)yesHuman-readable label
number_typevarchar(50)yesType of number (e.g., local, toll-free)
country_codevarchar(5)yesISO country code
voice_enabledbooleantruenoWhether voice calling is enabled
sms_enabledbooleanfalsenoWhether SMS is enabled
mms_enabledbooleanfalsenoWhether MMS is enabled
agent_mapping_iduuidyesFK → agent_mappings.id
directioncall_direction'inbound'yesDefault call direction
monthly_costnumeric(10,4)yesMonthly recurring cost
setup_costnumeric(10,4)yesOne-time purchase cost
currencyvarchar(3)'USD'noCost currency code
statusphone_number_status'pending'noLifecycle status
is_activebooleantruenoSoft-delete flag
purchased_attimestamptzyesWhen the number was purchased
released_attimestamptzyesWhen the number was released
created_attimestamptznow()noRecord creation timestamp
updated_attimestamptznow()noLast update timestamp (auto-updated)

Constraints:

ConstraintTypeDetails
agency_phone_numbers_pkeyPrimary keyid
agency_phone_numbers_phone_uniqueUniquephone_number
agency_phone_numbers_agency_id_fkeyForeign keyagency_idagencies(id) ON DELETE CASCADE
agency_phone_numbers_agent_mapping_id_fkeyForeign keyagent_mapping_idagent_mappings(id) ON DELETE SET NULL
agency_phone_numbers_phone_formatCheckphone_number ~ '^\+[1-9][0-9]{6,14}$' (E.164 format)

Indexes:

IndexColumnsNotes
idx_agency_phones_agency_idagency_idAgency-scoped queries
idx_agency_phones_phone_numberphone_numberLookup by number
idx_agency_phones_agent_mappingagent_mapping_idFiltered: WHERE agent_mapping_id IS NOT NULL
idx_agency_phones_statusstatusFilter by lifecycle status
idx_agency_phones_active(agency_id, is_active)Filtered: WHERE is_active = true

Triggers:

TriggerEventFunction
update_agency_phone_numbers_updated_atBEFORE UPDATEupdate_updated_at_column()

Related edge functions: phone-numbers-search, phone-numbers-purchase, phone-numbers-list, phone-numbers-assign, phone-numbers-release

Views: v_agency_phone_numbers_with_agent joins this table with agent_mappings to include agent name, voice, and language hint.