0

Send a message

by
Published Apr 8, 2025

Send a message with a Phone Number, Alphanumeric Sender ID, Short Code or Number Pool. This endpoint allows you to send a message with any messaging resource. Current messaging resources include: long-code, short-code, number-pool, and alphanumeric-sender-id.

Script telnyx Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Telnyx = {
3
	apiKey: string
4
}
5
/**
6
 * Send a message
7
 * Send a message with a Phone Number, Alphanumeric Sender ID, Short Code or Number Pool.
8

9
This endpoint allows you to send a message with any messaging resource.
10
Current messaging resources include: long-code, short-code, number-pool, and
11
alphanumeric-sender-id.
12

13
 */
14
export async function main(
15
	auth: Telnyx,
16
	body: {
17
		from?: string
18
		messaging_profile_id?: string
19
		to: string
20
		text?: string
21
		subject?: string
22
		media_urls?: string[]
23
		webhook_url?: string
24
		webhook_failover_url?: string
25
		use_profile_webhooks?: false | true
26
		type?: 'SMS' | 'MMS'
27
		auto_detect?: false | true
28
	}
29
) {
30
	const url = new URL(`https://api.telnyx.com/v2/messages`)
31

32
	const response = await fetch(url, {
33
		method: 'POST',
34
		headers: {
35
			'Content-Type': 'application/json',
36
			Authorization: 'Bearer ' + auth.apiKey
37
		},
38
		body: JSON.stringify(body)
39
	})
40
	if (!response.ok) {
41
		const text = await response.text()
42
		throw new Error(`${response.status} ${text}`)
43
	}
44
	return await response.json()
45
}
46