Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Design an App Using N1QL’s CURL Function

DZone's Guide to

Design an App Using N1QL’s CURL Function

In this article, we give a quick, but thorough walk through of N1QL's CURL function, and create a cool web-based map application.

· Web Dev Zone ·
Free Resource

Deploy code to production now. Release to users when ready. Learn how to separate code deployment from user-facing feature releases with LaunchDarkly.

With the addition of the CURL function in N1QL, interacting with external JSON endpoints and developer APIs such as the Google Maps API has become easier. To learn more about CURL(), check out this article

Let's see how to design a simple JavaScript application using CURL, the Google Maps API, and the Yelp Developer API to find certain types of local businesses that satisfy any input criteria within a certain distance from a given address.

Screen Shot 2017-07-24 at 11.28.09 PM.png

The prerequisite for running this app is to install and deploy the Couchbase server on your local machine. To deploy the app the user needs to enter three fields.

  1. The approximate address (what the search centers around).

  2. Search term (what the user is searching for).

  3. The radius of the search in miles.

The above image shows the deployed app. Here I wish to look for Ramen restaurants around my office within a 5-mile radius. It displays red pins on the map for all the results that match the search criteria, i.e. Ramen places within 5 miles of the Couchbase office at Mountain View. The green pin represents the input address. Now, I can individually click on the pins to display in-depth information for that particular place. This information includes the name, phone number, average rating, and a link to its Yelp page.

Let us now take a detailed look at the queries and JavaScript code to develop this app. For a detailed look at the code and to try it out see my GitHub page.

The JS app is broken down into several functions.

The initMap function initializes Google Maps to point to Australia and draws it. The getRadius and loadVal functions retrieve the input data to pass on to the loadDoc function. The loadDoc function sends a query and requests data from the N1QL server. This query is constructed using the CURL function and the input values.

It is first important to sign up and get a Yelp Auth header. The detailed steps on how to do this are given in their documentation.

After this, you can send requests to the Yelp server using their Search API. Here I use the term, location, and radius search parameters. The constructed N1QL query is:

select a.* 
from curl("https://api.yelp.com/v3/businesses/search?term=Ramen&location=2440%20West%20El%20Camino%20Real%2C%20Mountain%20View%2C%20CA&radius=16093",
          {"header":"Authorization: Bearer <YOUR_YELP_DEVELOPER_KEY> "}
         ) a;

This retrieves a list of businesses with different data such as their phone number, coordinates, address, etc. The following is the result of the above query.

{
    "requestID": "3ce019d3-0141-4371-9ace-4784c76a1f4d",
    "signature": {
        "*": "*"
    },
    "results": [
        {
            "businesses": [
                {
                    "categories": [
                        {
                            "alias": "ramen",
                            "title": "Ramen"
                        }
                    ],
                    "coordinates": {
                        "latitude": 37.4203109741211,
                        "longitude": -122.102348327637
                    },
                    "display_phone": "(650) 493-2878",
                    "distance": 2376.7542618019997,
                    "id": "dohatsuten-palo-alto",
                    "image_url": "https://s3-media1.fl.yelpcdn.com/bphoto/sLCWdM5Mx5P0pJt1_1DxAg/o.jpg",
                    "is_closed": false,
                    "location": {
                        "address1": "799 San Antonio Rd",
                        "address2": "",
                        "address3": "",
                        "city": "Palo Alto",
                        "country": "US",
                        "display_address": [
                            "799 San Antonio Rd",
                            "Palo Alto, CA 94303"
                        ],
                        "state": "CA",
                        "zip_code": "94303"
                    },
                    "name": "Dohatsuten",
                    "phone": "+16504932878",
                    "price": "$$",
                    "rating": 3.5,
                    "review_count": 643,
                    "transactions": [],
                    "url": "https://www.yelp.com/biz/dohatsuten-palo-alto?adjust_creative=yu6G_3_RJUnM9w3OCRdWUQ&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=yu6G_3_RJUnM9w3OCRdWUQ"
                },
                {
                    "categories": [
                        {
                            "alias": "ramen",
                            "title": "Ramen"
                        }
                    ],
                    "coordinates": {
                        "latitude": 37.393913,
                        "longitude": -122.079711
                    },
                    "display_phone": "(650) 965-8829",
                    "distance": 2544.737171002,
                    "id": "ramen-house-ryowa-mountain-view",
                    "image_url": "https://s3-media4.fl.yelpcdn.com/bphoto/ZLtukP2Dm0jUn3ESMDUaJg/o.jpg",
                    "is_closed": false,
                    "location": {
                        "address1": "859 Villa St",
                        "address2": "",
                        "address3": "",
                        "city": "Mountain View",
                        "country": "US",
                        "display_address": [
                            "859 Villa St",
                            "Mountain View, CA 94041"
                        ],
                        "state": "CA",
                        "zip_code": "94041"
                    },
                    "name": "Ramen House Ryowa",
                    "phone": "+16509658829",
                    "price": "$",
                    "rating": 3.5,
                    "review_count": 1579,
                    "transactions": [
                        "pickup"
                    ],
                    "url": "https://www.yelp.com/biz/ramen-house-ryowa-mountain-view?adjust_creative=yu6G_3_RJUnM9w3OCRdWUQ&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=yu6G_3_RJUnM9w3OCRdWUQ"
                },
                {
                    "categories": [
                        {
                            "alias": "ramen",
                            "title": "Ramen"
                        }
                    ],
                    "coordinates": {
                        "latitude": 37.3943154316007,
                        "longitude": -122.078879000335
                    },
                    "display_phone": "(650) 428-0888",
                    "distance": 2610.45891512,
                    "id": "ramen-izakaya-yu-gen-mountain-view",
                    "image_url": "https://s3-media1.fl.yelpcdn.com/bphoto/IXvdtfra5z6iU0HOvoasRQ/o.jpg",
                    "is_closed": false,
                    "location": {
                        "address1": "152 Castro St",
                        "address2": "",
                        "address3": "",
                        "city": "Mountain View",
                        "country": "US",
                        "display_address": [
                            "152 Castro St",
                            "Mountain View, CA 94041"
                        ],
                        "state": "CA",
                        "zip_code": "94041"
                    },
                    "name": "Ramen Izakaya Yu-Gen",
                    "phone": "+16504280888",
                    "price": "$$",
                    "rating": 3.5,
                    "review_count": 400,
                    "transactions": [],
                    "url": "https://www.yelp.com/biz/ramen-izakaya-yu-gen-mountain-view?adjust_creative=yu6G_3_RJUnM9w3OCRdWUQ&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=yu6G_3_RJUnM9w3OCRdWUQ"
                },
                {
                    "categories": [
                        {
                            "alias": "ramen",
                            "title": "Ramen"
                        },
                        {
                            "alias": "soup",
                            "title": "Soup"
                        },
                        {
                            "alias": "asianfusion",
                            "title": "Asian Fusion"
                        }
                    ],
                    "coordinates": {
                        "latitude": 37.391902,
                        "longitude": -122.0805266
                    },
                    "display_phone": "(650) 564-9931",
                    "distance": 2542.167698758,
                    "id": "maru-ichi-mountain-view",
                    "image_url": "https://s3-media1.fl.yelpcdn.com/bphoto/ZdkC8-WIEPIUkhwWVURfpQ/o.jpg",
                    "is_closed": false,
                    "location": {
                        "address1": "368 Castro St",
                        "address2": null,
                        "address3": "",
                        "city": "Mountain View",
                        "country": "US",
                        "display_address": [
                            "368 Castro St",
                            "Mountain View, CA 94041"
                        ],
                        "state": "CA",
                        "zip_code": "94041"
                    },
                    "name": "Maru Ichi",
                    "phone": "+16505649931",
                    "price": "$",
                    "rating": 3.5,
                    "review_count": 1686,
                    "transactions": [],
                    "url": "https://www.yelp.com/biz/maru-ichi-mountain-view?adjust_creative=yu6G_3_RJUnM9w3OCRdWUQ&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=yu6G_3_RJUnM9w3OCRdWUQ"
                },
                {
                    "categories": [
                        {
                            "alias": "asianfusion",
                            "title": "Asian Fusion"
                        }
                    ],
                    "coordinates": {
                        "latitude": 37.4107546553311,
                        "longitude": -122.093765354492
                    },
                    "display_phone": "(650) 964-3300",
                    "distance": 1768.0432938259999,
                    "id": "kumino-restaurant-mountain-view",
                    "image_url": "https://s3-media1.fl.yelpcdn.com/bphoto/9zpGZL2RMhABD3uyYLEnwg/o.jpg",
                    "is_closed": false,
                    "location": {
                        "address1": "580 N Rengstorff Ave",
                        "address2": "Ste J",
                        "address3": "",
                        "city": "Mountain View",
                        "country": "US",
                        "display_address": [
                            "580 N Rengstorff Ave",
                            "Ste J",
                            "Mountain View, CA 94043"
                        ],
                        "state": "CA",
                        "zip_code": "94043"
                    },
                    "name": "Kumino Restaurant",
                    "phone": "+16509643300",
                    "price": "$$",
                    "rating": 4,
                    "review_count": 283,
                    "transactions": [],
                    "url": "https://www.yelp.com/biz/kumino-restaurant-mountain-view?adjust_creative=yu6G_3_RJUnM9w3OCRdWUQ&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=yu6G_3_RJUnM9w3OCRdWUQ"
                },
                {
                    "categories": [
                        {
                            "alias": "noodles",
                            "title": "Noodles"
                        },
                        {
                            "alias": "ramen",
                            "title": "Ramen"
                        },
                        {
                            "alias": "dimsum",
                            "title": "Dim Sum"
                        }
                    ],
                    "coordinates": {
                        "latitude": 37.37962,
                        "longitude": -122.11695
                    },
                    "display_phone": "(650) 948-5700",
                    "distance": 2321.172164354,
                    "id": "charley-noodle-and-grill-los-altos-2",
                    "image_url": "https://s3-media3.fl.yelpcdn.com/bphoto/x_dA6MMgwXbkAIz_iFo7Hg/o.jpg",
                    "is_closed": false,
                    "location": {
                        "address1": "244 State St",
                        "address2": null,
                        "address3": "",
                        "city": "Los Altos",
                        "country": "US",
                        "display_address": [
                            "244 State St",
                            "Los Altos, CA 94022"
                        ],
                        "state": "CA",
                        "zip_code": "94022"
                    },
                    "name": "Charley Noodle & Grill",
                    "phone": "+16509485700",
                    "price": "$",
                    "rating": 4,
                    "review_count": 200,
                    "transactions": [],
                    "url": "https://www.yelp.com/biz/charley-noodle-and-grill-los-altos-2?adjust_creative=yu6G_3_RJUnM9w3OCRdWUQ&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=yu6G_3_RJUnM9w3OCRdWUQ"
                },
                {
                    "categories": [
                        {
                            "alias": "ramen",
                            "title": "Ramen"
                        },
                        {
                            "alias": "noodles",
                            "title": "Noodles"
                        }
                    ],
                    "coordinates": {
                        "latitude": 37.39224,
                        "longitude": -122.07824
                    },
                    "display_phone": "(650) 965-8001",
                    "distance": 2712.237921938,
                    "id": "shalala-mountain-view-2",
                    "image_url": "https://s3-media2.fl.yelpcdn.com/bphoto/Dr9dqo_EZRTs5i8jLI1epQ/o.jpg",
                    "is_closed": false,
                    "location": {
                        "address1": "698 W Dana St",
                        "address2": "",
                        "address3": "",
                        "city": "Mountain View",
                        "country": "US",
                        "display_address": [
                            "698 W Dana St",
                            "Mountain View, CA 94041"
                        ],
                        "state": "CA",
                        "zip_code": "94041"
                    },
                    "name": "Shalala",
                    "phone": "+16509658001",
                    "price": "$$",
                    "rating": 3.5,
                    "review_count": 810,
                    "transactions": [
                        "pickup"
                    ],
                    "url": "https://www.yelp.com/biz/shalala-mountain-view-2?adjust_creative=yu6G_3_RJUnM9w3OCRdWUQ&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=yu6G_3_RJUnM9w3OCRdWUQ"
                },
                {
                    "categories": [
                        {
                            "alias": "ramen",
                            "title": "Ramen"
                        }
                    ],
                    "coordinates": {
                        "latitude": 37.3828735351562,
                        "longitude": -121.995086669922
                    },
                    "display_phone": "(408) 245-7779",
                    "distance": 10098.20938368,
                    "id": "myzen-japanese-ramen-sunnyvale",
                    "image_url": "https://s3-media4.fl.yelpcdn.com/bphoto/Kh8vsuQ1RtvXxwBShHEVag/o.jpg",
                    "is_closed": false,
                    "location": {
                        "address1": "520 Lawrence Expwy",
                        "address2": "Ste 304",
                        "address3": "",
                        "city": "Sunnyvale",
                        "country": "US",
                        "display_address": [
                            "520 Lawrence Expwy",
                            "Ste 304",
                            "Sunnyvale, CA 94085"
                        ],
                        "state": "CA",
                        "zip_code": "94085"
                    },
                    "name": "Myzen Ramen",
                    "phone": "+14082457779",
                    "price": "$$",
                    "rating": 4,
                    "review_count": 460,
                    "transactions": [],
                    "url": "https://www.yelp.com/biz/myzen-japanese-ramen-sunnyvale?adjust_creative=yu6G_3_RJUnM9w3OCRdWUQ&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=yu6G_3_RJUnM9w3OCRdWUQ"
                },
                {
                    "categories": [
                        {
                            "alias": "sushi",
                            "title": "Sushi Bars"
                        },
                        {
                            "alias": "ramen",
                            "title": "Ramen"
                        }
                    ],
                    "coordinates": {
                        "latitude": 37.402973,
                        "longitude": -122.106628
                    },
                    "display_phone": "(650) 948-8388",
                    "distance": 408.1798492922,
                    "id": "sushi-88-and-ramen-mountain-view",
                    "image_url": "https://s3-media2.fl.yelpcdn.com/bphoto/uqrslzCQp3Bclhgso23woQ/o.jpg",
                    "is_closed": false,
                    "location": {
                        "address1": "506 Showers Dr",
                        "address2": "",
                        "address3": "",
                        "city": "Mountain View",
                        "country": "US",
                        "display_address": [
                            "506 Showers Dr",
                            "Mountain View, CA 94040"
                        ],
                        "state": "CA",
                        "zip_code": "94040"
                    },
                    "name": "Sushi 88 & Ramen",
                    "phone": "+16509488388",
                    "price": "$$",
                    "rating": 3.5,
                    "review_count": 363,
                    "transactions": [
                        "pickup",
                        "delivery"
                    ],
                    "url": "https://www.yelp.com/biz/sushi-88-and-ramen-mountain-view?adjust_creative=yu6G_3_RJUnM9w3OCRdWUQ&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=yu6G_3_RJUnM9w3OCRdWUQ"
                },
                {
                    "categories": [
                        {
                            "alias": "ramen",
                            "title": "Ramen"
                        }
                    ],
                    "coordinates": {
                        "latitude": 37.3765680151228,
                        "longitude": -122.030557001692
                    },
                    "display_phone": "(408) 481-0900",
                    "distance": 7273.9988953639995,
                    "id": "ramen-seas-sunnyvale",
                    "image_url": "https://s3-media3.fl.yelpcdn.com/bphoto/XNrK6MD2bu1p6BT9qoHQng/o.jpg",
                    "is_closed": false,
                    "location": {
                        "address1": "173 S Murphy Ave",
                        "address2": "",
                        "address3": "",
                        "city": "Sunnyvale",
                        "country": "US",
                        "display_address": [
                            "173 S Murphy Ave",
                            "Sunnyvale, CA 94086"
                        ],
                        "state": "CA",
                        "zip_code": "94086"
                    },
                    "name": "Ramen Seas",
                    "phone": "+14084810900",
                    "price": "$$",
                    "rating": 4,
                    "review_count": 555,
                    "transactions": [],
                    "url": "https://www.yelp.com/biz/ramen-seas-sunnyvale?adjust_creative=yu6G_3_RJUnM9w3OCRdWUQ&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=yu6G_3_RJUnM9w3OCRdWUQ"
                },
                {
                    "categories": [
                        {
                            "alias": "ramen",
                            "title": "Ramen"
                        }
                    ],
                    "coordinates": {
                        "latitude": 37.3363484055083,
                        "longitude": -121.994188273627
                    },
                    "display_phone": "(408) 246-2955",
                    "distance": 12235.905361719999,
                    "id": "orenchi-ramen-santa-clara",
                    "image_url": "https://s3-media1.fl.yelpcdn.com/bphoto/ovCGJ_AkwB-VhOrpix8vtA/o.jpg",
                    "is_closed": false,
                    "location": {
                        "address1": "3540 Homestead Rd",
                        "address2": "",
                        "address3": "",
                        "city": "Santa Clara",
                        "country": "US",
                        "display_address": [
                            "3540 Homestead Rd",
                            "Santa Clara, CA 95051"
                        ],
                        "state": "CA",
                        "zip_code": "95051"
                    },
                    "name": "Orenchi Ramen",
                    "phone": "+14082462955",
                    "price": "$$",
                    "rating": 4,
                    "review_count": 4585,
                    "transactions": [],
                    "url": "https://www.yelp.com/biz/orenchi-ramen-santa-clara?adjust_creative=yu6G_3_RJUnM9w3OCRdWUQ&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=yu6G_3_RJUnM9w3OCRdWUQ"
                },
                {
                    "categories": [
                        {
                            "alias": "japanese",
                            "title": "Japanese"
                        }
                    ],
                    "coordinates": {
                        "latitude": 37.3791032284498,
                        "longitude": -122.116447538137
                    },
                    "display_phone": "(650) 917-1822",
                    "distance": 2365.253918162,
                    "id": "sumika-los-altos",
                    "image_url": "https://s3-media2.fl.yelpcdn.com/bphoto/W4aCSyUPx4BTmyQqZSumBg/o.jpg",
                    "is_closed": false,
                    "location": {
                        "address1": "236 Central Plz",
                        "address2": null,
                        "address3": "",
                        "city": "Los Altos",
                        "country": "US",
                        "display_address": [
                            "236 Central Plz",
                            "Los Altos, CA 94022"
                        ],
                        "state": "CA",
                        "zip_code": "94022"
                    },
                    "name": "Sumika",
                    "phone": "+16509171822",
                    "price": "$$",
                    "rating": 4,
                    "review_count": 814,
                    "transactions": [],
                    "url": "https://www.yelp.com/biz/sumika-los-altos?adjust_creative=yu6G_3_RJUnM9w3OCRdWUQ&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=yu6G_3_RJUnM9w3OCRdWUQ"
                },
                {
                    "categories": [
                        {
                            "alias": "ramen",
                            "title": "Ramen"
                        }
                    ],
                    "coordinates": {
                        "latitude": 37.31022,
                        "longitude": -122.03182
                    },
                    "display_phone": "(408) 343-1148",
                    "distance": 11961.1540089,
                    "id": "mori-kitchen-san-jose",
                    "image_url": "https://s3-media1.fl.yelpcdn.com/bphoto/Yomk0xwQ96oRFl8Ayd1Krw/o.jpg",
                    "is_closed": false,
                    "location": {
                        "address1": "1000 S De Anza Blvd",
                        "address2": "",
                        "address3": "",
                        "city": "San Jose",
                        "country": "US",
                        "display_address": [
                            "1000 S De Anza Blvd",
                            "San Jose, CA 95129"
                        ],
                        "state": "CA",
                        "zip_code": "95129"
                    },
                    "name": "Mori Kitchen",
                    "phone": "+14083431148",
                    "price": "$$",
                    "rating": 3.5,
                    "review_count": 282,
                    "transactions": [],
                    "url": "https://www.yelp.com/biz/mori-kitchen-san-jose?adjust_creative=yu6G_3_RJUnM9w3OCRdWUQ&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=yu6G_3_RJUnM9w3OCRdWUQ"
                },
                {
                    "categories": [
                        {
                            "alias": "ramen",
                            "title": "Ramen"
                        },
                        {
                            "alias": "noodles",
                            "title": "Noodles"
                        },
                        {
                            "alias": "asianfusion",
                            "title": "Asian Fusion"
                        }
                    ],
                    "coordinates": {
                        "latitude": 37.3149669768623,
                        "longitude": -121.977912953967
                    },
                    "display_phone": "(408) 446-1101",
                    "distance": 14819.446455999998,
                    "id": "hokkaido-ramen-santouka-san-jose",
                    "image_url": "https://s3-media2.fl.yelpcdn.com/bphoto/Fd8-2gLRjF9I5e54R_F1qQ/o.jpg",
                    "is_closed": false,
                    "location": {
                        "address1": "675 Saratoga Ave",
                        "address2": "",
                        "address3": "",
                        "city": "San Jose",
                        "country": "US",
                        "display_address": [
                            "675 Saratoga Ave",
                            "San Jose, CA 95129"
                        ],
                        "state": "CA",
                        "zip_code": "95129"
                    },
                    "name": "Hokkaido Ramen Santouka",
                    "phone": "+14084461101",
                    "price": "$$",
                    "rating": 4,
                    "review_count": 2577,
                    "transactions": [],
                    "url": "https://www.yelp.com/biz/hokkaido-ramen-santouka-san-jose?adjust_creative=yu6G_3_RJUnM9w3OCRdWUQ&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=yu6G_3_RJUnM9w3OCRdWUQ"
                },
                {
                    "categories": [
                        {
                            "alias": "japanese",
                            "title": "Japanese"
                        },
                        {
                            "alias": "sushi",
                            "title": "Sushi Bars"
                        }
                    ],
                    "coordinates": {
                        "latitude": 37.4270973205566,
                        "longitude": -122.14794921875
                    },
                    "display_phone": "(650) 327-2222",
                    "distance": 4687.823955239999,
                    "id": "odori-sushi-and-teppanyaki-palo-alto",
                    "image_url": "https://s3-media2.fl.yelpcdn.com/bphoto/IzoMT5ITHHSLjjOQmVh4XQ/o.jpg",
                    "is_closed": false,
                    "location": {
                        "address1": "2111 El Camino Real",
                        "address2": "",
                        "address3": "",
                        "city": "Palo Alto",
                        "country": "US",
                        "display_address": [
                            "2111 El Camino Real",
                            "Palo Alto, CA 94306"
                        ],
                        "state": "CA",
                        "zip_code": "94306"
                    },
                    "name": "Odori Sushi and Teppanyaki",
                    "phone": "+16503272222",
                    "price": "$$",
                    "rating": 4,
                    "review_count": 366,
                    "transactions": [
                        "pickup"
                    ],
                    "url": "https://www.yelp.com/biz/odori-sushi-and-teppanyaki-palo-alto?adjust_creative=yu6G_3_RJUnM9w3OCRdWUQ&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=yu6G_3_RJUnM9w3OCRdWUQ"
                },
                {
                    "categories": [
                        {
                            "alias": "japanese",
                            "title": "Japanese"
                        },
                        {
                            "alias": "delis",
                            "title": "Delis"
                        }
                    ],
                    "coordinates": {
                        "latitude": 37.3788284376314,
                        "longitude": -122.071364298463
                    },
                    "display_phone": "(650) 691-1600",
                    "distance": 3938.7814360759994,
                    "id": "nijiya-deli-mountain-view",
                    "image_url": "https://s3-media2.fl.yelpcdn.com/bphoto/JpIDusNZMNpZ4I_AQ2ukIw/o.jpg",
                    "is_closed": false,
                    "location": {
                        "address1": "143 E El Camino Real",
                        "address2": "",
                        "address3": "",
                        "city": "Mountain View",
                        "country": "US",
                        "display_address": [
                            "143 E El Camino Real",
                            "Mountain View, CA 94040"
                        ],
                        "state": "CA",
                        "zip_code": "94040"
                    },
                    "name": "Nijiya Deli",
                    "phone": "+16506911600",
                    "price": "$",
                    "rating": 4.5,
                    "review_count": 36,
                    "transactions": [],
                    "url": "https://www.yelp.com/biz/nijiya-deli-mountain-view?adjust_creative=yu6G_3_RJUnM9w3OCRdWUQ&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=yu6G_3_RJUnM9w3OCRdWUQ"
                },
                {
                    "categories": [
                        {
                            "alias": "korean",
                            "title": "Korean"
                        },
                        {
                            "alias": "bbq",
                            "title": "Barbeque"
                        }
                    ],
                    "coordinates": {
                        "latitude": 37.41364,
                        "longitude": -122.12543
                    },
                    "display_phone": "(650) 424-8805",
                    "distance": 2217.740043488,
                    "id": "so-gong-dong-tofu-house-palo-alto",
                    "image_url": "https://s3-media2.fl.yelpcdn.com/bphoto/BkcEVPouwybMklFG6mPvBA/o.jpg",
                    "is_closed": false,
                    "location": {
                        "address1": "4127 El Camino Real",
                        "address2": null,
                        "address3": "",
                        "city": "Palo Alto",
                        "country": "US",
                        "display_address": [
                            "4127 El Camino Real",
                            "Palo Alto, CA 94306"
                        ],
                        "state": "CA",
                        "zip_code": "94306"
                    },
                    "name": "So Gong Dong Tofu House",
                    "phone": "+16504248805",
                    "price": "$$",
                    "rating": 4,
                    "review_count": 1215,
                    "transactions": [],
                    "url": "https://www.yelp.com/biz/so-gong-dong-tofu-house-palo-alto?adjust_creative=yu6G_3_RJUnM9w3OCRdWUQ&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=yu6G_3_RJUnM9w3OCRdWUQ"
                },
                {
                    "categories": [
                        {
                            "alias": "japanese",
                            "title": "Japanese"
                        },
                        {
                            "alias": "sushi",
                            "title": "Sushi Bars"
                        },
                        {
                            "alias": "seafood",
                            "title": "Seafood"
                        }
                    ],
                    "coordinates": {
                        "latitude": 37.3944392,
                        "longitude": -122.0787435
                    },
                    "display_phone": "(650) 386-6821",
                    "distance": 2619.38109608,
                    "id": "bushido-izakaya-restaurant-mountain-view",
                    "image_url": "https://s3-media4.fl.yelpcdn.com/bphoto/PvWztH-jqqtsLUXVnRNHsA/o.jpg",
                    "is_closed": false,
                    "location": {
                        "address1": "156 Castro St",
                        "address2": "",
                        "address3": "",
                        "city": "Mountain View",
                        "country": "US",
                        "display_address": [
                            "156 Castro St",
                            "Mountain View, CA 94041"
                        ],
                        "state": "CA",
                        "zip_code": "94041"
                    },
                    "name": "Bushido Izakaya Restaurant",
                    "phone": "+16503866821",
                    "price": "$$",
                    "rating": 3.5,
                    "review_count": 668,
                    "transactions": [],
                    "url": "https://www.yelp.com/biz/bushido-izakaya-restaurant-mountain-view?adjust_creative=yu6G_3_RJUnM9w3OCRdWUQ&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=yu6G_3_RJUnM9w3OCRdWUQ"
                },
                {
                    "categories": [
                        {
                            "alias": "ramen",
                            "title": "Ramen"
                        },
                        {
                            "alias": "sushi",
                            "title": "Sushi Bars"
                        }
                    ],
                    "coordinates": {
                        "latitude": 37.44496,
                        "longitude": -122.16292
                    },
                    "display_phone": "(650) 324-3300",
                    "distance": 7013.42486234,
                    "id": "ichimi-ramen-and-rolls-palo-alto",
                    "image_url": "https://s3-media2.fl.yelpcdn.com/bphoto/2sIQkvS_kNGyIlDvOc550Q/o.jpg",
                    "is_closed": false,
                    "location": {
                        "address1": "201 University Ave",
                        "address2": "",
                        "address3": null,
                        "city": "Palo Alto",
                        "country": "US",
                        "display_address": [
                            "201 University Ave",
                            "Palo Alto, CA 94301"
                        ],
                        "state": "CA",
                        "zip_code": "94301"
                    },
                    "name": "Ichimi Ramen & Rolls",
                    "phone": "+16503243300",
                    "price": "$$",
                    "rating": 3,
                    "review_count": 27,
                    "transactions": [],
                    "url": "https://www.yelp.com/biz/ichimi-ramen-and-rolls-palo-alto?adjust_creative=yu6G_3_RJUnM9w3OCRdWUQ&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=yu6G_3_RJUnM9w3OCRdWUQ"
                },
                {
                    "categories": [
                        {
                            "alias": "sushi",
                            "title": "Sushi Bars"
                        },
                        {
                            "alias": "japanese",
                            "title": "Japanese"
                        }
                    ],
                    "coordinates": {
                        "latitude": 37.3915303378286,
                        "longitude": -122.07806645178
                    },
                    "display_phone": "(650) 968-3227",
                    "distance": 2761.785638122,
                    "id": "sushi-tomi-mountain-view-2",
                    "image_url": "https://s3-media2.fl.yelpcdn.com/bphoto/5BhjdLIoFFA2iwx8XIZ7TA/o.jpg",
                    "is_closed": false,
                    "location": {
                        "address1": "635 W Dana St",
                        "address2": "",
                        "address3": "",
                        "city": "Mountain View",
                        "country": "US",
                        "display_address": [
                            "635 W Dana St",
                            "Mountain View, CA 94040"
                        ],
                        "state": "CA",
                        "zip_code": "94040"
                    },
                    "name": "Sushi Tomi",
                    "phone": "+16509683227",
                    "price": "$$",
                    "rating": 4,
                    "review_count": 1800,
                    "transactions": [],
                    "url": "https://www.yelp.com/biz/sushi-tomi-mountain-view-2?adjust_creative=yu6G_3_RJUnM9w3OCRdWUQ&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=yu6G_3_RJUnM9w3OCRdWUQ"
                }
            ],
            "region": {
                "center": {
                    "latitude": 37.39933519916451,
                    "longitude": -122.1078872680664
                }
            },
            "total": 260
        }
    ],
    "status": "success",
    "metrics": {
        "elapsedTime": "540.178185ms",
        "executionTime": "540.160243ms",
        "resultCount": 1,
        "resultSize": 36934
    }
}

The result is the parsed into JSON.

Sample entry for coordinates -
"coordinates": {
                        "latitude": 37.3915303378286,
                        "longitude": -122.07806645178
                    }

The coordinates (latitude and longitude) from each document are then plotted onto Google Maps using the Google Map JavaScript API using the addMarker function (which adds markers to Google Maps for each entry).

To see how to use the Google Maps JavaScript API, refer to their documentation.

Now that we can access all this data using the CURL function, it is straightforward to load the data into a Couchbase bucket and run different queries on it. This operation is accomplished using the INSERT statement in N1QL.

insert into default (key UUID(), VALUE a) 
select a 
from curl("https://api.yelp.com/v3/businesses/search?term=Ramen&location=2440%20West%20El%20Camino%20Real%2C%20Mountain%20View%2C%20CA&radius=16093",
          {"header":"Authorization: Bearer <YOUR_YELP_API_AUTH_HEADER>"}).businesses a;

The above query will insert 20 documents into the default bucket in your Couchbase instance.

For details about the security aspects of the CURL function, please refer to this article.

Summary

With this example, we demonstrated how one can use the CURL function and integrate different JSON endpoints with Couchbase to develop simple apps.

Deploy code to production now. Release to users when ready. Learn how to separate code deployment from user-facing feature releases with LaunchDarkly.

Topics:
n1ql ,couchbase ,curl ,api ,web dev

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}