Skip to content

Instantly share code, notes, and snippets.

@vickyRathee
Last active August 25, 2024 14:10
Show Gist options
  • Save vickyRathee/79dce7284feea1d78d439d8b318fad31 to your computer and use it in GitHub Desktop.
Save vickyRathee/79dce7284feea1d78d439d8b318fad31 to your computer and use it in GitHub Desktop.
Integrate the idm.in URL shortener API with Node.js to automatically shorten URLs and send SMS messages via the Twilio API
const fetch = require('node-fetch');
const twilio = require('twilio');
// Twilio credentials
const accountSid = 'your_twilio_account_sid';
const authToken = 'your_twilio_auth_token';
const client = new twilio(accountSid, authToken);
// IDM.in API credentials
const IDM_API_KEY = 'your_idm_api_key'; // https://app.idm.in/settings/apikeys
async function shortenUrl(longUrl) {
const response = await fetch('https://api.idm.in/v1/links', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${IDM_API_KEY}`
},
body: JSON.stringify({ url: longUrl })
});
const data = await response.json();
if (response.ok) {
return data.short_url;
}
}
async function sendSms(to, from, longUrl, messageBody) {
const shortUrl = await shortenUrl(longUrl);
const fullMessage = `${messageBody} ${shortUrl}`;
client.messages
.create({
body: fullMessage,
from: from,
to: to
})
.then(message => console.log(`Message sent: ${message.sid}`))
.catch(error => console.error('Failed to send SMS:', error));
}
// Example usage
const to = '+1234567890';
const from = '+0987654321';
const longUrl = 'https://dayschedule.com/appointemnt/12312312/confirmed';
const messageBody = 'Check out this link:';
sendSms(to, from, longUrl, messageBody);
@vickyRathee
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment