Skip to content

Instantly share code, notes, and snippets.

@magician11
Last active September 18, 2023 14:39
Show Gist options
  • Save magician11/37ee3c5a47be7655d3ef1601f6263194 to your computer and use it in GitHub Desktop.
Save magician11/37ee3c5a47be7655d3ef1601f6263194 to your computer and use it in GitHub Desktop.
How to use Request with the Google URL Shortener API.
const request = require('request');
const GOOGLE_API_KEY = 'your api key'; // from https://console.developers.google.com/apis/credentials
const urlToShorten = 'http://www.thelongurltoshorten.com';
const shortenerUrl = `https://www.googleapis.com/urlshortener/v1/url?key=${GOOGLE_API_KEY}`;
const options = {
uri: shortenerUrl,
json: {
longUrl: urlToShorten
}
};
request.post(options, (error, response, body) => {
if (body.error) {
console.log(body.error);
} else if (response.statusCode === 200) {
console.log(body);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment