{"_id":"588f722cbcace50f0052ba11","__v":0,"category":{"_id":"588f722bbcace50f0052b9e4","__v":0,"project":"565f5fa26bafd40d0030a064","version":"588f722bbcace50f0052b9e1","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"},"project":"565f5fa26bafd40d0030a064","version":{"_id":"588f722bbcace50f0052b9e1","project":"565f5fa26bafd40d0030a064","__v":1,"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"},"parentDoc":null,"user":"565f3941ea46251700972783","updates":["57e54dfd685f7c19007fba4c","57ea8b4c6b8ec33200f63a7b"],"next":{"pages":[],"description":""},"createdAt":"2015-12-28T23:26:41.288Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":3,"body":"[block:callout]\n{\n  \"type\": \"info\",\n  \"title\": \"Important\",\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.justapis.com/v5.0.0/samples.json\\\">Sample APIs</a> we provide.\\n\\nThe instructions in this tutorial refer to the sample Proxy Endpoint called “Multiple remote endpoints - sequential calls workflow” 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}\n[/block]\nThis tutorial shows how to create a Proxy Endpoint with multiple Remote Endpoints called in a sequence: (1) a token validation call, and (2) a data call. The workflow validates the token then makes a call which returns a single Alert object as the response.\n\n ## Step 1\n\nThis Proxy Endpoint requires you to first define two Remote Endpoints: “Validate Token” and “Alerts – GET”.\n\nWithin the sample APIs Remote Endpoint list, find “Validate Token”, which is meant to simulate a service that checks whether a given token is valid, and click the row.\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/H0L25H2AQoKgZCUHeOn6_samples-and-tutorials-multiple-remote-endpoints-in-sequence-step1.png\",\n        \"samples-and-tutorials-multiple-remote-endpoints-in-sequence-step1.png\",\n        \"3360\",\n        \"716\",\n        \"#6585a9\",\n        \"\"\n      ]\n    }\n  ]\n}\n[/block]\n## Step 2\n\nWhen you are in edit mode, you will see that this Remote Endpoint has a few properties that were specified when it was created:\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/60DEYPqLQ4O1RAOeu6BR_samples-and-tutorials-multiple-remote-endpoints-in-sequence-step2.png\",\n        \"samples-and-tutorials-multiple-remote-endpoints-in-sequence-step2.png\",\n        \"3354\",\n        \"1740\",\n        \"#5c6c8c\",\n        \"\"\n      ]\n    }\n  ]\n}\n[/block]\n1. The Remote Endpoint’s **name** is: “Validate Token”, which is used for display purposes in the Admin Web App.\n\n2. The Remote Endpoint’s **codename** is: “validateToken”, which determines how the Remote Endpoint \nRequest and Response Objects are referred to within JavaScript logic blocks.\n\n3. The Remote Endpoint **URL**: <a target=\"_blank\" href=\"http://justapis-mock.herokuapp.com/v1/validate_token\">http://justapis-mock.herokuapp.com/v1/validate_token</a>. This is the URL of the external API endpoint that you are calling.\n\n4. The **Method** of external API endpoint to validate token is GET.\n\n5. There are no **headers**, **query parameters** and **environment data** required to be set for this Remote Endpoint.\n\n## Step 3\n\nNow find the second Remote Endpoint called “Alerts - GET”, which returns a single JSON alert object, and click the row.\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/toFGmZF8Tky9pbnz0niK_samples-and-tutorials-multiple-remote-endpoints-in-sequence-step3.png\",\n        \"samples-and-tutorials-multiple-remote-endpoints-in-sequence-step3.png\",\n        \"3360\",\n        \"838\",\n        \"#5389c5\",\n        \"\"\n      ]\n    }\n  ]\n}\n[/block]\n## Step 4\n\nWhen you are in edit mode, you will see that this Remote Endpoint has a few properties that were specified when it was created.\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/GIBtiP3So6wsRGBk2gcw_samples-and-tutorials-multiple-remote-endpoints-in-sequence-step4.png\",\n        \"samples-and-tutorials-multiple-remote-endpoints-in-sequence-step4.png\",\n        \"3360\",\n        \"1774\",\n        \"#8d9b54\",\n        \"\"\n      ]\n    }\n  ]\n}\n[/block]\n1. The Remote Endpoint’s **name** is: “Alerts - GET”, which is used for display purposes in the Admin Web App.\n\n2. The Remote Endpoint’s **codename** is: “alerts”, which determines how the Remote Endpoint Request and Response Objects are referred to within JavaScript logic blocks.\n\n3. The Remote Endpoint **URL**: <a target=\"_blank\" href=\"http://justapis-mock.herokuapp.com/v1/alerts\">http://justapis-mock.herokuapp.com/v1/alerts</a>. This is the URL of the external API endpoint that you are calling.\n\n4. The **Method** of external API endpoint to validate token is GET.\n\n5. There are no **headers**, **query parameters** and **environment data** required to be set for this Remote Endpoint.\n\n## Step 5\n\nNow you are ready to define the Proxy Endpoint that calls these two Remote Endpoints in sequence. Find the “Multiple remote endpoints - sequential calls workflow”, 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/2H5owLRZRLJjHC4C6oun_samples-and-tutorials-multiple-remote-endpoints-in-sequence-step5.png\",\n        \"samples-and-tutorials-multiple-remote-endpoints-in-sequence-step5.png\",\n        \"3350\",\n        \"732\",\n        \"#5b2f3d\",\n        \"\"\n      ]\n    }\n  ]\n}\n[/block]\n## Step 6\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/7IFd1QNiTDmTRLSnylcn_samples-and-tutorials-multiple-remote-endpoints-in-sequence-step6.png\",\n        \"samples-and-tutorials-multiple-remote-endpoints-in-sequence-step6.png\",\n        \"3360\",\n        \"1660\",\n        \"#bc8e4d\",\n        \"\"\n      ]\n    }\n  ]\n}\n[/block]\n1. The Proxy Endpoint’s **name** is: “Multiple remote endpoints - sequential calls”, 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 “Multi-call” **Group**.\n\n5. There is one **Route** defined, which is a GET method as “/auth/alerts”. This means that if the Host is organic-apparel-7319.justapis.io, the full URL for this Proxy Endpoint is <a target=\"_blank\" href=\"https://organic-apparel-7319.justapis.io/auth/alerts\">https://organic-apparel-7319.justapis.io/auth/alerts</a>.\n\nNote that there are two steps in this Proxy Endpoint Workflow, which are Single Call Components, as denoted by the following icon.\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/R3TAn860SpieSjIl0q8P_samples-and-tutorials-multiple-remote-endpoints-in-sequence-single-call-component-icon.png\",\n        \"samples-and-tutorials-multiple-remote-endpoints-in-sequence-single-call-component-icon.png\",\n        \"1280\",\n        \"91\",\n        \"#4c647c\",\n        \"\"\n      ]\n    }\n  ]\n}\n[/block]\n## Step 7\n\nClick on the first Single Call Component icon to view the details of this step in the workflow.\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/HGbMWDftTbyElTk3wkJe_samples-and-tutorials-multiple-remote-endpoints-in-sequence-step7.png\",\n        \"samples-and-tutorials-multiple-remote-endpoints-in-sequence-step7.png\",\n        \"3360\",\n        \"1816\",\n        \"#309ad8\",\n        \"\"\n      ]\n    }\n  ]\n}\n[/block]\nYou will see that this Single Call 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. The Conditional Logic block has the following JavaScript:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"_.isEmpty(request.params.token && request.headers.Token);\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\nThis checks to see if the token is empty on both URL params and headers.\n \n3. The Before Request logic block is empty.\n\n4. This first Single Call component calls the Remote Endpoint “Validate Token” defined in step 2 above.\n\n5. The Endpoint name override has been left blank.\n\n6. The After request logic block has the following JavaScript. This logic is executed to manipulate the response.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"response.statusCode === 204 ? validToken = true : validToken = false;\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\nThis code sets the **validToken** to true if Remote Endpoint is successful or false if not.\n\n## Step 8\n\nClick on the second Single Call Component icon to view the details of this step in the workflow.\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/fhFjP81tQkeajpH2dOXY_samples-and-tutorials-multiple-remote-endpoints-in-sequence-step8.png\",\n        \"samples-and-tutorials-multiple-remote-endpoints-in-sequence-step8.png\",\n        \"3360\",\n        \"1894\",\n        \"#309ad7\",\n        \"\"\n      ]\n    }\n  ]\n}\n[/block]\nYou will see that this Single Call 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. The Conditional Logic block has the following JavaScript.\n\nThis checks to see if validToken call is true. Note: developers can leverage autocomplete feature here to define the JavaScript Object.\n\n3. The Before Request logic block is empty.\n\n4. This second Single Call component calls the Remote Endpoint “Alerts - GET” defined in step 3 above.\n\n5. The Endpoint name override has been left blank.\n\n6. The After request logic block has the following JavaScript. This logic is executed to manipulate the response:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"response.headers[\\\"Content-Type\\\"] = \\\"application/json\\\";\\nvalidToken !== true ? statusCode = 401 : '';\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\nThe first line sets the response header Content-Type to JSON and the second line checks to see if validToken is true if it is set statusCode to 401.\n\n## Step 9\n\nNow let us test this Proxy Endpoint. Open a new browser window or tab, and invoke the Proxy Endpoint using the following URLs (if you have changed the default Host, please use your new hostname instead).\n\nInvalid call test: <a target=\"_blank\" href=\"https://organic-apparel-7319.justapis.io/auth/alerts?token=54321\">https://organic-apparel-7319.justapis.io/auth/alerts?token=54321</a>\n\nYou should see the resulting message in your browser window.\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/XbL4tX9YQBWplDyibX63_invalid-token.png\",\n        \"invalid-token.png\",\n        \"617\",\n        \"192\",\n        \"#b19242\",\n        \"\"\n      ]\n    }\n  ]\n}\n[/block]\nValid call test: <a target=\"_blank\" href=\"https://organic-apparel-7319.justapis.io/auth/alerts?token=12345\">https://organic-apparel-7319.justapis.io/auth/alerts?token=12345</a>\n\nYou should see the resulting message in your browser window.\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/GGrchPGTAGvRGqHrNneN_valid-token.png\",\n        \"valid-token.png\",\n        \"1717\",\n        \"743\",\n        \"#368832\",\n        \"\"\n      ]\n    }\n  ]\n}\n[/block]\nCongratulations, you have completed this tutorial.","excerpt":"","slug":"multiple-remote-endpoints-in-sequence","type":"basic","title":"Multiple Remote Endpoints in Sequence"}

Multiple Remote Endpoints in Sequence


[block:callout] { "type": "info", "title": "Important", "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.justapis.com/v5.0.0/samples.json\">Sample APIs</a> we provide.\n\nThe instructions in this tutorial refer to the sample Proxy Endpoint called “Multiple remote endpoints - sequential calls workflow” 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." } [/block] This tutorial shows how to create a Proxy Endpoint with multiple Remote Endpoints called in a sequence: (1) a token validation call, and (2) a data call. The workflow validates the token then makes a call which returns a single Alert object as the response. ## Step 1 This Proxy Endpoint requires you to first define two Remote Endpoints: “Validate Token” and “Alerts – GET”. Within the sample APIs Remote Endpoint list, find “Validate Token”, which is meant to simulate a service that checks whether a given token is valid, and click the row. [block:image] { "images": [ { "image": [ "https://files.readme.io/H0L25H2AQoKgZCUHeOn6_samples-and-tutorials-multiple-remote-endpoints-in-sequence-step1.png", "samples-and-tutorials-multiple-remote-endpoints-in-sequence-step1.png", "3360", "716", "#6585a9", "" ] } ] } [/block] ## Step 2 When you are in edit mode, you will see that this Remote Endpoint has a few properties that were specified when it was created: [block:image] { "images": [ { "image": [ "https://files.readme.io/60DEYPqLQ4O1RAOeu6BR_samples-and-tutorials-multiple-remote-endpoints-in-sequence-step2.png", "samples-and-tutorials-multiple-remote-endpoints-in-sequence-step2.png", "3354", "1740", "#5c6c8c", "" ] } ] } [/block] 1. The Remote Endpoint’s **name** is: “Validate Token”, which is used for display purposes in the Admin Web App. 2. The Remote Endpoint’s **codename** is: “validateToken”, which determines how the Remote Endpoint Request and Response Objects are referred to within JavaScript logic blocks. 3. The Remote Endpoint **URL**: <a target="_blank" href="http://justapis-mock.herokuapp.com/v1/validate_token">http://justapis-mock.herokuapp.com/v1/validate_token</a>. This is the URL of the external API endpoint that you are calling. 4. The **Method** of external API endpoint to validate token is GET. 5. There are no **headers**, **query parameters** and **environment data** required to be set for this Remote Endpoint. ## Step 3 Now find the second Remote Endpoint called “Alerts - GET”, which returns a single JSON alert object, and click the row. [block:image] { "images": [ { "image": [ "https://files.readme.io/toFGmZF8Tky9pbnz0niK_samples-and-tutorials-multiple-remote-endpoints-in-sequence-step3.png", "samples-and-tutorials-multiple-remote-endpoints-in-sequence-step3.png", "3360", "838", "#5389c5", "" ] } ] } [/block] ## Step 4 When you are in edit mode, you will see that this Remote Endpoint has a few properties that were specified when it was created. [block:image] { "images": [ { "image": [ "https://files.readme.io/GIBtiP3So6wsRGBk2gcw_samples-and-tutorials-multiple-remote-endpoints-in-sequence-step4.png", "samples-and-tutorials-multiple-remote-endpoints-in-sequence-step4.png", "3360", "1774", "#8d9b54", "" ] } ] } [/block] 1. The Remote Endpoint’s **name** is: “Alerts - GET”, which is used for display purposes in the Admin Web App. 2. The Remote Endpoint’s **codename** is: “alerts”, which determines how the Remote Endpoint Request and Response Objects are referred to within JavaScript logic blocks. 3. The Remote Endpoint **URL**: <a target="_blank" href="http://justapis-mock.herokuapp.com/v1/alerts">http://justapis-mock.herokuapp.com/v1/alerts</a>. This is the URL of the external API endpoint that you are calling. 4. The **Method** of external API endpoint to validate token is GET. 5. There are no **headers**, **query parameters** and **environment data** required to be set for this Remote Endpoint. ## Step 5 Now you are ready to define the Proxy Endpoint that calls these two Remote Endpoints in sequence. Find the “Multiple remote endpoints - sequential calls workflow”, which should appear in the list of Proxy Endpoints, and click the row. [block:image] { "images": [ { "image": [ "https://files.readme.io/2H5owLRZRLJjHC4C6oun_samples-and-tutorials-multiple-remote-endpoints-in-sequence-step5.png", "samples-and-tutorials-multiple-remote-endpoints-in-sequence-step5.png", "3350", "732", "#5b2f3d", "" ] } ] } [/block] ## Step 6 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/7IFd1QNiTDmTRLSnylcn_samples-and-tutorials-multiple-remote-endpoints-in-sequence-step6.png", "samples-and-tutorials-multiple-remote-endpoints-in-sequence-step6.png", "3360", "1660", "#bc8e4d", "" ] } ] } [/block] 1. The Proxy Endpoint’s **name** is: “Multiple remote endpoints - sequential calls”, 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 “Multi-call” **Group**. 5. There is one **Route** defined, which is a GET method as “/auth/alerts”. This means that if the Host is organic-apparel-7319.justapis.io, the full URL for this Proxy Endpoint is <a target="_blank" href="https://organic-apparel-7319.justapis.io/auth/alerts">https://organic-apparel-7319.justapis.io/auth/alerts</a>. Note that there are two steps in this Proxy Endpoint Workflow, which are Single Call Components, as denoted by the following icon. [block:image] { "images": [ { "image": [ "https://files.readme.io/R3TAn860SpieSjIl0q8P_samples-and-tutorials-multiple-remote-endpoints-in-sequence-single-call-component-icon.png", "samples-and-tutorials-multiple-remote-endpoints-in-sequence-single-call-component-icon.png", "1280", "91", "#4c647c", "" ] } ] } [/block] ## Step 7 Click on the first Single Call Component icon to view the details of this step in the workflow. [block:image] { "images": [ { "image": [ "https://files.readme.io/HGbMWDftTbyElTk3wkJe_samples-and-tutorials-multiple-remote-endpoints-in-sequence-step7.png", "samples-and-tutorials-multiple-remote-endpoints-in-sequence-step7.png", "3360", "1816", "#309ad8", "" ] } ] } [/block] You will see that this Single Call 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. The Conditional Logic block has the following JavaScript: [block:code] { "codes": [ { "code": "_.isEmpty(request.params.token && request.headers.Token);", "language": "javascript" } ] } [/block] This checks to see if the token is empty on both URL params and headers. 3. The Before Request logic block is empty. 4. This first Single Call component calls the Remote Endpoint “Validate Token” defined in step 2 above. 5. The Endpoint name override has been left blank. 6. The After request logic block has the following JavaScript. This logic is executed to manipulate the response. [block:code] { "codes": [ { "code": "response.statusCode === 204 ? validToken = true : validToken = false;", "language": "javascript" } ] } [/block] This code sets the **validToken** to true if Remote Endpoint is successful or false if not. ## Step 8 Click on the second Single Call Component icon to view the details of this step in the workflow. [block:image] { "images": [ { "image": [ "https://files.readme.io/fhFjP81tQkeajpH2dOXY_samples-and-tutorials-multiple-remote-endpoints-in-sequence-step8.png", "samples-and-tutorials-multiple-remote-endpoints-in-sequence-step8.png", "3360", "1894", "#309ad7", "" ] } ] } [/block] You will see that this Single Call 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. The Conditional Logic block has the following JavaScript. This checks to see if validToken call is true. Note: developers can leverage autocomplete feature here to define the JavaScript Object. 3. The Before Request logic block is empty. 4. This second Single Call component calls the Remote Endpoint “Alerts - GET” defined in step 3 above. 5. The Endpoint name override has been left blank. 6. The After request logic block has the following JavaScript. This logic is executed to manipulate the response: [block:code] { "codes": [ { "code": "response.headers[\"Content-Type\"] = \"application/json\";\nvalidToken !== true ? statusCode = 401 : '';", "language": "javascript" } ] } [/block] The first line sets the response header Content-Type to JSON and the second line checks to see if validToken is true if it is set statusCode to 401. ## Step 9 Now let us test this Proxy Endpoint. Open a new browser window or tab, and invoke the Proxy Endpoint using the following URLs (if you have changed the default Host, please use your new hostname instead). Invalid call test: <a target="_blank" href="https://organic-apparel-7319.justapis.io/auth/alerts?token=54321">https://organic-apparel-7319.justapis.io/auth/alerts?token=54321</a> You should see the resulting message in your browser window. [block:image] { "images": [ { "image": [ "https://files.readme.io/XbL4tX9YQBWplDyibX63_invalid-token.png", "invalid-token.png", "617", "192", "#b19242", "" ] } ] } [/block] Valid call test: <a target="_blank" href="https://organic-apparel-7319.justapis.io/auth/alerts?token=12345">https://organic-apparel-7319.justapis.io/auth/alerts?token=12345</a> You should see the resulting message in your browser window. [block:image] { "images": [ { "image": [ "https://files.readme.io/GGrchPGTAGvRGqHrNneN_valid-token.png", "valid-token.png", "1717", "743", "#368832", "" ] } ] } [/block] Congratulations, you have completed this tutorial.