How to reduce plastic waste in your daily life

Plastic waste is a major environmental problem that affects both land and marine ecosystems. It takes hundreds of years to break down and decompose in the environment, and in the process, it releases…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Make a Simple Google API call with Node.js

Learn how to make an API call and create a Command Line Address to Geographic Coordinate Converter App.

Welcome to this introductory tutorial to Node.js and API calls!

Before we jump into the Google API setup, I want to clarify what an API is and what it does.

API — Application Programming Interface

3. Click + Create a new project and enter a name for your project. (Node App works fine)

4. You should now be on the Google Cloud Platform Console. Click on the APIs button in the side menu.

5. Under Enabled APIs, click the Geocoding API

6. Once on the Geocoding API page, you should see three tabs under the title. Click on Credentials.

7. You should now see a list of your API keys. Make note of the Key.

It is now time to setup the project!

2. Navigate inside of the directory ‘cd node-project’ and run

3. Name the project simple-node-project

4. For version - hit enter

5. Description isn’t required, but you may add one if you like.

6. For entry point - hit enter

7. For test command - hit enter

8. For git repository - hit enter

9. For keywords - hit enter

10. For author - enter your name

11. For license - hit enter

12. Is this OK? (yes) - hit enter

13. Create a file named index.js. You may use which ever editor you wish. For those of you who don’t know what Vim or Emacs is, just open the node-project directory and create a new file. You may edit the file in any text editor you wish. Make sure the file is named index.js.

Your project is now setup and you are almost ready to write your first line of code!

To make the API call we will be using the node module request. This node module makes API calling much easier. To install the node module run the following command:

If there is an error with the API call, then the error will be displayed with the error variable. If no errors were present, then the response from the call will be stored in the body variable.

Now that we have the request created and setup, let’s add some values to make it usable.

I hope you kept that Google API key noted somewhere! This is where we will put it to use. The code below is how we will declare and initialize the necessary variables. You will need to insert this code above the request call to ensure the variables are all declared and initialized before using them.

What is happening above?

Your code should now be ready to make the call and get a response. It should now look like the following:

Your code can now be run in the terminal by running the following command:

Congrats! You have just run your first Command Line Node.js application that makes a call to the Google Geocoding API. Now you may be wondering what the funky output is. The response from the Google API is returned in JSON format so the output you are seeing is displayed in JSON format.

As you can see the output from your program is a bit hard to read. That is because it is being printed out in JSON format, which is essentially just an acronym for JavaScript Object Notation meaning it formats a JavaScript object into something that is readable.

Now let’s fix the response to only display the latitude and longitude. You will need the following code:

First thing that we had to do was convert the crazy text that is returned, into a JavaScript Object that we can use. So, we create the variable geolocation and initialize it with the JavaScript Object of the returned text by using:

Next we create a message to be returned. We use back ticks since we want to add our latitude and longitude values. You can use dot notation to grab the latitude and longitude values from within the JavaScript Object that we saved into the variable geoLocation. Dot notation is simply a path to the value you are wanting from inside of an object.

Last, but not least we display the message using:

Your code should now look like the following:

Add a comment

Related posts:

Exerpeutic LX7 Indoor Cycle Trainer

If you want to more challenging cardiovascular fitness workout and looking for an indoor spin bike with a lot of features then Exerpeutic LX7 Indoor Cycle Trainer is gonna be your best mate. Its…