{"__v":0,"_id":"588f722cbcace50f0052ba0e","category":{"version":"588f722bbcace50f0052b9e1","project":"565f5fa26bafd40d0030a064","_id":"588f722bbcace50f0052b9e4","__v":0,"sync":{"url":"","isSync":false},"reference":false,"createdAt":"2015-12-26T05:04:48.794Z","from_sync":false,"order":2,"slug":"samples-and-tutorials","title":"How Tos & Examples"},"parentDoc":null,"project":"565f5fa26bafd40d0030a064","user":"565f3941ea46251700972783","version":{"__v":1,"_id":"588f722bbcace50f0052b9e1","project":"565f5fa26bafd40d0030a064","createdAt":"2017-01-30T17:04:43.410Z","releaseDate":"2017-01-30T17:04:43.410Z","categories":["588f722bbcace50f0052b9e2","588f722bbcace50f0052b9e3","588f722bbcace50f0052b9e4","588f722bbcace50f0052b9e5","588f722bbcace50f0052b9e6","588f722bbcace50f0052b9e7"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"5.3.0","version":"5.3.0"},"updates":["57e54fcff3d7fc0e009c50ee","57ea8ac86b8ec33200f63a7a"],"next":{"pages":[],"description":""},"createdAt":"2015-12-26T05:08:15.433Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":0,"body":"[block:callout]\n{\n  \"type\": \"info\",\n  \"body\": \"In order to run these examples, please make sure you've followed the [Quick Start Guide](doc:quick-start-guide). You'll need to have signed up with <a target=\\\"_blank\\\" href=\\\"https://dashboard.nanoscale.io/#/registration\\\">nanoscale.io</a> and downloaded the <a target=\\\"_blank\\\" href=\\\"http://downloads.nanoscale.io/latest/samples.json\\\">Sample APIs</a> we provide.\\n\\nThe instructions in this tutorial refer to the sample Proxy Endpoint called “A JSON Hello World proxy” in the sample API that you imported in the Quick Start Guide steps, and assumes you have a nanoscale.io account.  If you have not imported the sample API or signed up for <a target=\\\"_blank\\\" href=\\\"https://dashboard.nanoscale.io/#/registration\\\">nanoscale.io</a>, please do so now.\",\n  \"title\": \"Important\"\n}\n[/block]\nThis tutorial show how to create a standalone Proxy Endpoint that uses JavaScript Logic Component to return a simple “Hello World” message with JSON format.\n \n## Step 1\n\nFind the “A JSON Hello World Proxy”, which should appear in the list of Proxy Endpoints, and click the row.\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/zvj47jJRViIAYOJ3k9qL_samples-and-tutorials-create-first-js-proxy-endpoint-step1.png\",\n        \"samples-and-tutorials-create-first-js-proxy-endpoint-step1.png\",\n        \"3338\",\n        \"994\",\n        \"#749b57\",\n        \"\"\n      ]\n    }\n  ]\n}\n[/block]\n## Step 2\n\nWhen you are in edit mode, you will see that this Proxy Endpoint has several properties that were specified when it was created.\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/AamGbPztRcOv9p9F4FsU_samples-and-tutorials-create-first-js-proxy-endpoint-step2.png\",\n        \"samples-and-tutorials-create-first-js-proxy-endpoint-step2.png\",\n        \"3360\",\n        \"1482\",\n        \"#64acfc\",\n        \"\"\n      ]\n    }\n  ]\n}\n[/block]\n1. The Proxy Endpoint’s **name** is: “A JSON Hello World Proxy”, which is used for display purposes in the Admin Web App.\n\n2. The **Active** status is set to on, which means that this Proxy Endpoint will run if the server receives a request for it.\n\n3. This Proxy Endpoint is assigned to the “Dev” **Environment**, and any environment variables used in will be populated from the “Dev” environment.\n\n4. This Proxy Endpoint is assigned to the “Hello World” **Group**..\n\n5. There is one **Route** defined, which is a Get method at “/helloworld.json”. This means that if the Host on this API is organic-apparel-7319.justapis.io, the full URL for this Proxy Endpoint is <a target=\"_blank\" href=\"https://organic-apparel-7319.justapis.io/helloworld.json\">https://organic-apparel-7319.justapis.io/helloworld.json</a>.\n\nNote that there is one step in this Proxy Endpoint Workflow, which is a Logic Component, as denoted by the following icon.\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/PINQSwZ3Ta1eiAJnHpSA_samples-and-tutorials-create-first-js-proxy-endpoint-logic-component.png\",\n        \"samples-and-tutorials-create-first-js-proxy-endpoint-logic-component.png\",\n        \"1280\",\n        \"82\",\n        \"#4d647c\",\n        \"\"\n      ]\n    }\n  ]\n}\n[/block]\n## Step 3\n\nClick on the Logic Component icon to view the details of the first step in the workflow.\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/znKhhYMeQ32KSN1QiDS7_samples-and-tutorials-create-first-js-proxy-endpoint-step3.png\",\n        \"samples-and-tutorials-create-first-js-proxy-endpoint-step3.png\",\n        \"3360\",\n        \"1596\",\n        \"#6c87a8\",\n        \"\"\n      ]\n    }\n  ]\n}\n[/block]\nYou will see that this Logic Component is comprised of several sub-steps.\n \n1. Conditional set to “If”, which means that any JavaScript logic specified in the Conditional Logic block must evaluate to “true” to continue. Otherwise, the workflow will skip to the end.\n\n2. There is no Conditional Logic specified, so the workflow will move to the next sub-step.\n\n3. The Logic block has two lines of JavaScript. The first line is:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"response.headers[\\\"Content-Type\\\"] = \\\"application/json\\\";\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\nThis first line specifies that the Response Object’s HTTP header “Content-Type” should be set to the value “application/json”. This is standard setting when returning JSON formatted responses via HTTP, and is what you will typically use when creating RESTful API responses.\n \nThe second line sets the body of the Response Object:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"response.body = JSON.stringify({message: \\\"Hello world! Your nanoscale.io instance is running correctly.\\\"});\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\nThis line uses a JavaScript helper method called JSON.stringify that converts a JavaScript value to a JSON string (more information <a target=\"_blank\" href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify\">here</a>). The actual string we are returning is “{message: “Hello world! Your nanoscale.io instance is running correctly.”}”.\n\n## Step 4\n\nNow let's test the endpoint! Visit your Host and Proxy Endpoint URL combination to verify the endpoint works and is publicly defined. You should see something like the image below.\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/pQkz7BsiQPiPNcDPEFoT_helloworld.png\",\n        \"helloworld.png\",\n        \"687\",\n        \"248\",\n        \"#2e613c\",\n        \"\"\n      ]\n    }\n  ]\n}\n[/block]\nCongratulations, you have completed this tutorial.","excerpt":"","slug":"create-your-first-proxy-endpoint","type":"basic","title":"Create Your First Proxy Endpoint"}

Create Your First Proxy Endpoint


[block:callout] { "type": "info", "body": "In order to run these examples, please make sure you've followed the [Quick Start Guide](doc:quick-start-guide). You'll need to have signed up with <a target=\"_blank\" href=\"https://dashboard.nanoscale.io/#/registration\">nanoscale.io</a> and downloaded the <a target=\"_blank\" href=\"http://downloads.nanoscale.io/latest/samples.json\">Sample APIs</a> we provide.\n\nThe instructions in this tutorial refer to the sample Proxy Endpoint called “A JSON Hello World proxy” in the sample API that you imported in the Quick Start Guide steps, and assumes you have a nanoscale.io account. If you have not imported the sample API or signed up for <a target=\"_blank\" href=\"https://dashboard.nanoscale.io/#/registration\">nanoscale.io</a>, please do so now.", "title": "Important" } [/block] This tutorial show how to create a standalone Proxy Endpoint that uses JavaScript Logic Component to return a simple “Hello World” message with JSON format. ## Step 1 Find the “A JSON Hello World Proxy”, which should appear in the list of Proxy Endpoints, and click the row. [block:image] { "images": [ { "image": [ "https://files.readme.io/zvj47jJRViIAYOJ3k9qL_samples-and-tutorials-create-first-js-proxy-endpoint-step1.png", "samples-and-tutorials-create-first-js-proxy-endpoint-step1.png", "3338", "994", "#749b57", "" ] } ] } [/block] ## Step 2 When you are in edit mode, you will see that this Proxy Endpoint has several properties that were specified when it was created. [block:image] { "images": [ { "image": [ "https://files.readme.io/AamGbPztRcOv9p9F4FsU_samples-and-tutorials-create-first-js-proxy-endpoint-step2.png", "samples-and-tutorials-create-first-js-proxy-endpoint-step2.png", "3360", "1482", "#64acfc", "" ] } ] } [/block] 1. The Proxy Endpoint’s **name** is: “A JSON Hello World Proxy”, which is used for display purposes in the Admin Web App. 2. The **Active** status is set to on, which means that this Proxy Endpoint will run if the server receives a request for it. 3. This Proxy Endpoint is assigned to the “Dev” **Environment**, and any environment variables used in will be populated from the “Dev” environment. 4. This Proxy Endpoint is assigned to the “Hello World” **Group**.. 5. There is one **Route** defined, which is a Get method at “/helloworld.json”. This means that if the Host on this API is organic-apparel-7319.justapis.io, the full URL for this Proxy Endpoint is <a target="_blank" href="https://organic-apparel-7319.justapis.io/helloworld.json">https://organic-apparel-7319.justapis.io/helloworld.json</a>. Note that there is one step in this Proxy Endpoint Workflow, which is a Logic Component, as denoted by the following icon. [block:image] { "images": [ { "image": [ "https://files.readme.io/PINQSwZ3Ta1eiAJnHpSA_samples-and-tutorials-create-first-js-proxy-endpoint-logic-component.png", "samples-and-tutorials-create-first-js-proxy-endpoint-logic-component.png", "1280", "82", "#4d647c", "" ] } ] } [/block] ## Step 3 Click on the Logic Component icon to view the details of the first step in the workflow. [block:image] { "images": [ { "image": [ "https://files.readme.io/znKhhYMeQ32KSN1QiDS7_samples-and-tutorials-create-first-js-proxy-endpoint-step3.png", "samples-and-tutorials-create-first-js-proxy-endpoint-step3.png", "3360", "1596", "#6c87a8", "" ] } ] } [/block] You will see that this Logic Component is comprised of several sub-steps. 1. Conditional set to “If”, which means that any JavaScript logic specified in the Conditional Logic block must evaluate to “true” to continue. Otherwise, the workflow will skip to the end. 2. There is no Conditional Logic specified, so the workflow will move to the next sub-step. 3. The Logic block has two lines of JavaScript. The first line is: [block:code] { "codes": [ { "code": "response.headers[\"Content-Type\"] = \"application/json\";", "language": "javascript" } ] } [/block] This first line specifies that the Response Object’s HTTP header “Content-Type” should be set to the value “application/json”. This is standard setting when returning JSON formatted responses via HTTP, and is what you will typically use when creating RESTful API responses. The second line sets the body of the Response Object: [block:code] { "codes": [ { "code": "response.body = JSON.stringify({message: \"Hello world! Your nanoscale.io instance is running correctly.\"});", "language": "javascript" } ] } [/block] This line uses a JavaScript helper method called JSON.stringify that converts a JavaScript value to a JSON string (more information <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify">here</a>). The actual string we are returning is “{message: “Hello world! Your nanoscale.io instance is running correctly.”}”. ## Step 4 Now let's test the endpoint! Visit your Host and Proxy Endpoint URL combination to verify the endpoint works and is publicly defined. You should see something like the image below. [block:image] { "images": [ { "image": [ "https://files.readme.io/pQkz7BsiQPiPNcDPEFoT_helloworld.png", "helloworld.png", "687", "248", "#2e613c", "" ] } ] } [/block] Congratulations, you have completed this tutorial.