type Contiguity = {
token: string
}
export async function main(
resource: Contiguity,
body: {
to: string
message: string
}
) {
const endpoint = 'https://api.contiguity.co/send/text'
// Validate phone number format
const phoneRegex = /^\+[1-9]\d{0,14}$/
if (!phoneRegex.test(body.to)) {
throw new Error(
'Invalid phone number format. Please use international format (e.g. +12065551234 or +923001234567).'
)
}
const response = await fetch(endpoint, {
method: 'POST',
headers: {
Authorization: `Token ${resource.token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(body)
})
if (!response.ok) {
const errorData = await response.json()
throw new Error(`HTTP error! status: ${response.status}, message: ${JSON.stringify(errorData)}`)
}
const data = await response.json()
return data
}
Submitted by hugo697 118 days ago