0
Send SMS
One script reply has been approved by the moderators Verified

Send a text message

Created by hugo697 25 days ago Viewed 11 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 25 days ago
1
type Contiguity = {
2
	token: string
3
}
4

5
export async function main(
6
	resource: Contiguity,
7
	body: {
8
		to: string
9
		message: string
10
	}
11
) {
12
	const endpoint = 'https://api.contiguity.co/send/text'
13

14
	// Validate phone number format
15
	const phoneRegex = /^\+[1-9]\d{0,14}$/
16

17
	if (!phoneRegex.test(body.to)) {
18
		throw new Error(
19
			'Invalid phone number format. Please use international format (e.g. +12065551234 or +923001234567).'
20
		)
21
	}
22

23
	const response = await fetch(endpoint, {
24
		method: 'POST',
25
		headers: {
26
			Authorization: `Token ${resource.token}`,
27
			'Content-Type': 'application/json'
28
		},
29
		body: JSON.stringify(body)
30
	})
31

32
	if (!response.ok) {
33
		const errorData = await response.json()
34
		throw new Error(`HTTP error! status: ${response.status}, message: ${JSON.stringify(errorData)}`)
35
	}
36

37
	const data = await response.json()
38

39
	return data
40
}
41