javascriptnode.jsgoogle-apigoogle-url-shortener

Is there any good node modules to connect google shortener API to shorten a long URL


I am working with Node.js , and trying to shorten a long URL , i am trying to connect to google shortener API , if found in the API documentation for Node.js

google-api-nodejs-client

but it only has codes for convert the short URL to a long URL ,

And i found node-google-url-shorter

this doesn't use an API key , since i may send over 10,000 request per day this is not good and it is not a maintaining repo i think .

and this one google-url , seems this one is depicted as well .

So is there any other good node modules .

I want to shorten a long URL .

Thank you in advance :)


Solution

  • I will answer my own question since there are no answers , i found a node module .

    googleapis-plus

    to get a shorter URL you have to call

    client.urlshortener.url.insert
    

    Here is an example

      function get_shorter_url(config,long_url,callback) {
    
        var googleapis  = require('googleapis-plus');
    
        var api_key = "your api key";
        var long_url = "your long url";
    
            try {
    
                    googleapis
                        .discover('urlshortener', 'v1')
                        .execute(function(err, client) {
    
                            if( err) { // handle discovery errors
    
                                callback(err);
                            } else {
    
                                var getShortUrl = client.urlshortener.url.insert({ longUrl: long_url })
                                    .withApiKey(api_key);
    
                                getShortUrl.execute(function(err, shortUrlDetails) {
    
                                    if(err) {
    
                                        callback(err);
                                    } else {
    
                                        callback(null,shortUrlDetails.id);
                                    }
                                });
                            }
                        });
    
                } catch ( err ) {
    
                    console.log("there was an exception "+err);
                    callback(err, null);
                }
        }