{"__v":0,"_id":"588f722cbcace50f0052ba14","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":["57e54cb603746919001f0d45","57ea8ba0802e4a17009181aa"],"next":{"pages":[],"description":""},"createdAt":"2016-01-01T00:56:30.964Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":6,"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 “CRUD - Create an event” 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 that invokes a Remote Endpoint to show an event record.\n \n## Step 1\n\nThis Proxy Endpoint requires you to first define a Remote Endpoint: “Event – POST”\nWithin the sample APIs Remote Endpoint list, find “Event - GET”, which is meant to simulate a service to read event information. Click the edit (pencil) icon that appears when you hover over the row.\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/C1Ve8sG4RE2MfDuuZgNh_samples-and-tutorials-create-a-proxy-endpoint-that-invokes-a-remote-endpoint-to-create-a-record-step1.png\",\n        \"samples-and-tutorials-create-a-proxy-endpoint-that-invokes-a-remote-endpoint-to-create-a-record-step1.png\",\n        \"3328\",\n        \"1260\",\n        \"#5489c6\",\n        \"\"\n      ]\n    }\n  ]\n}\n[/block]\nStep 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/0UcO2P5QRUO5J0lsk9Zr_samples-and-tutorials-create-a-proxy-endpoint-that-invokes-a-remote-endpoint-to-create-a-record-step2.png\",\n        \"samples-and-tutorials-create-a-proxy-endpoint-that-invokes-a-remote-endpoint-to-create-a-record-step2.png\",\n        \"3326\",\n        \"1490\",\n        \"#e09a41\",\n        \"\"\n      ]\n    }\n  ]\n}\n[/block]\n1. The Remote Endpoint’s **name** is: “Event - POST”, which is used for display purposes in the Admin Web App.\n\n2. The Remote Endpoint’s **codename** is: “createEvent”, which determines how the Remote Endpoint Request and Response Objects are referred to within JavaScript logic blocks.\n\n3. The Remote Endpoint **URL** has been set to “<a target=\"_blank\" href=\"http://justapis-mock.herokuapp.com/v1/events/create\">http://justapis-mock.herokuapp.com/v1/events/create</a>”. \nThis is the URL of the external API endpoint that you are calling.\n\n4. The **Method** of external API endpoint to validate token is POST.\n\n5. A **header** has been defined to pass basic auth credentials required by the Remote Endpoint.\n\n6. There are no **query parameters** and **environment data** required to be set for this Remote Endpoint.\n\n## Step 3\n\nNow you are ready to define the Proxy Endpoint that the above defined Remote Endpoint to show the data. Find the “CRUD - Create an event”, 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/hWO3d21QQQ2ni9DB253s_samples-and-tutorials-create-a-proxy-endpoint-that-invokes-a-remote-endpoint-to-create-a-record-step3.png\",\n        \"samples-and-tutorials-create-a-proxy-endpoint-that-invokes-a-remote-endpoint-to-create-a-record-step3.png\",\n        \"3332\",\n        \"1258\",\n        \"#6f9b58\",\n        \"\"\n      ]\n    }\n  ]\n}\n[/block]\n## Step 4\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/uFo3fihVR1WtwD9IweqM_samples-and-tutorials-create-a-proxy-endpoint-that-invokes-a-remote-endpoint-to-create-a-record-step4.png\",\n        \"samples-and-tutorials-create-a-proxy-endpoint-that-invokes-a-remote-endpoint-to-create-a-record-step4.png\",\n        \"3326\",\n        \"1456\",\n        \"#74a468\",\n        \"\"\n      ]\n    }\n  ]\n}\n[/block]\n1. The Proxy Endpoint’s **name** is: “CRUD - Create an event”, 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 “Event calls” **Group**.\n\n5. There is one **Route** defined, which is a GET method as “/events”. 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/events\">https://organic-apparel-7319.justapis.io/events</a>.\n\nNote that there is one step in this Proxy Endpoint Workflow, which is a Single Call Component, as denoted by the following icon.\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/ogVyMNbTQUGxNzTLxaBE_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 5\n\nClick on the 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/KAEZBRxRMyDji99kqahR_samples-and-tutorials-create-a-proxy-endpoint-that-invokes-a-remote-endpoint-to-create-a-record-step5.png\",\n        \"samples-and-tutorials-create-a-proxy-endpoint-that-invokes-a-remote-endpoint-to-create-a-record-step5.png\",\n        \"3326\",\n        \"1500\",\n        \"#2b9ad6\",\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 code.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"response.statusCode = 422;\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\nThis code returns 422 if the data is invalid (name is required).\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"response.headers[\\\"Content-Type\\\"] = \\\"application/json\\\";\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\nThis code returns a JSON string.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"response.body = JSON.stringify({error : \\\"Missing required fields : Name\\\"});\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\nThis code returns an error message.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"if(request.body){\\n  try{\\n    JSON.parse(request.body).name;\\n  }catch(err){\\n    false;\\n  }\\n}else{\\n  false;\\n}\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\nThis code makes sure that if name isn't included in create then stop the execution, else move to Before request logic block.\n\n3. The Before Request logic block has the following JavaScript code.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"var body = JSON.parse(request.body);\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\nThis code grabs the request body.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"if(!body.start_date){\\n  body.start_date = new Date().toLocaleString();\\n}\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\n This code checks if there is no start date, it sets start date to today.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"if((body.status !== 'closed' && body.status !== 'open') || !body.status  ){\\n\\tbody.status = \\\"open\\\";\\n}\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\nThis code checks if the status is missing or not been set to either \"open\", or \"closed\", then sets it to \"open\".\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"if(body.end_date){\\n  body.endDate = body.end_date;\\n  delete body.end_date;\\n}\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\nThis code changes underscore key to camel case key.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"request.body = JSON.stringify(body);\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\n  This code adds manipulated body to the request body.\n \n4. This Single Call component calls the Remote Endpoint “Event - POST” 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\": \"var response_data = JSON.parse(response.body).response.results;\\nresponse.body = JSON.stringify(response_data);\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\nThis code drills down to the results object in the response.\n\n## Step 6\n\nNow let us test this Proxy Endpoint. Open a tool (such as Postman) where you can run a POST call to create a record. Run the following POST call with the body as shown below.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"curl -H \\\"Content-Type: application/json\\\" -X POST -d '{\\\"name\\\": \\\"Event 10\\\",\\\"start_date\\\":\\\"2015-09-09\\\"}' http://lyrical-icicle-3650.justapis-staging.io/events\",\n      \"language\": \"curl\"\n    }\n  ]\n}\n[/block]\nYou should see a message such as this.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"{\\\"id\\\": 107,\\\"name\\\": \\\"Event 10\\\",\\\"start_date\\\": \\\"2015-09-09\\\",\\\"status\\\": \\\"open\\\"}\",\n      \"language\": \"json\"\n    }\n  ]\n}\n[/block]\nCongratulations, you have completed this tutorial.","excerpt":"","slug":"proxy-endpoint-that-invokes-a-remote-endpoint-to-create-a-record","type":"basic","title":"Proxy Endpoint That Invokes a Remote Endpoint To Create a Record"}

Proxy Endpoint That Invokes a Remote Endpoint To Create a Record


[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 “CRUD - Create an event” 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 that invokes a Remote Endpoint to show an event record. ## Step 1 This Proxy Endpoint requires you to first define a Remote Endpoint: “Event – POST” Within the sample APIs Remote Endpoint list, find “Event - GET”, which is meant to simulate a service to read event information. Click the edit (pencil) icon that appears when you hover over the row. [block:image] { "images": [ { "image": [ "https://files.readme.io/C1Ve8sG4RE2MfDuuZgNh_samples-and-tutorials-create-a-proxy-endpoint-that-invokes-a-remote-endpoint-to-create-a-record-step1.png", "samples-and-tutorials-create-a-proxy-endpoint-that-invokes-a-remote-endpoint-to-create-a-record-step1.png", "3328", "1260", "#5489c6", "" ] } ] } [/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/0UcO2P5QRUO5J0lsk9Zr_samples-and-tutorials-create-a-proxy-endpoint-that-invokes-a-remote-endpoint-to-create-a-record-step2.png", "samples-and-tutorials-create-a-proxy-endpoint-that-invokes-a-remote-endpoint-to-create-a-record-step2.png", "3326", "1490", "#e09a41", "" ] } ] } [/block] 1. The Remote Endpoint’s **name** is: “Event - POST”, which is used for display purposes in the Admin Web App. 2. The Remote Endpoint’s **codename** is: “createEvent”, which determines how the Remote Endpoint Request and Response Objects are referred to within JavaScript logic blocks. 3. The Remote Endpoint **URL** has been set to “<a target="_blank" href="http://justapis-mock.herokuapp.com/v1/events/create">http://justapis-mock.herokuapp.com/v1/events/create</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 POST. 5. A **header** has been defined to pass basic auth credentials required by the Remote Endpoint. 6. There are no **query parameters** and **environment data** required to be set for this Remote Endpoint. ## Step 3 Now you are ready to define the Proxy Endpoint that the above defined Remote Endpoint to show the data. Find the “CRUD - Create an event”, which should appear in the list of Proxy Endpoints, and click the row. [block:image] { "images": [ { "image": [ "https://files.readme.io/hWO3d21QQQ2ni9DB253s_samples-and-tutorials-create-a-proxy-endpoint-that-invokes-a-remote-endpoint-to-create-a-record-step3.png", "samples-and-tutorials-create-a-proxy-endpoint-that-invokes-a-remote-endpoint-to-create-a-record-step3.png", "3332", "1258", "#6f9b58", "" ] } ] } [/block] ## Step 4 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/uFo3fihVR1WtwD9IweqM_samples-and-tutorials-create-a-proxy-endpoint-that-invokes-a-remote-endpoint-to-create-a-record-step4.png", "samples-and-tutorials-create-a-proxy-endpoint-that-invokes-a-remote-endpoint-to-create-a-record-step4.png", "3326", "1456", "#74a468", "" ] } ] } [/block] 1. The Proxy Endpoint’s **name** is: “CRUD - Create an event”, 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 “Event calls” **Group**. 5. There is one **Route** defined, which is a GET method as “/events”. 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/events">https://organic-apparel-7319.justapis.io/events</a>. Note that there is one step in this Proxy Endpoint Workflow, which is a Single Call Component, as denoted by the following icon. [block:image] { "images": [ { "image": [ "https://files.readme.io/ogVyMNbTQUGxNzTLxaBE_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 5 Click on the Single Call Component icon to view the details of this step in the workflow. [block:image] { "images": [ { "image": [ "https://files.readme.io/KAEZBRxRMyDji99kqahR_samples-and-tutorials-create-a-proxy-endpoint-that-invokes-a-remote-endpoint-to-create-a-record-step5.png", "samples-and-tutorials-create-a-proxy-endpoint-that-invokes-a-remote-endpoint-to-create-a-record-step5.png", "3326", "1500", "#2b9ad6", "" ] } ] } [/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 code. [block:code] { "codes": [ { "code": "response.statusCode = 422;", "language": "javascript" } ] } [/block] This code returns 422 if the data is invalid (name is required). [block:code] { "codes": [ { "code": "response.headers[\"Content-Type\"] = \"application/json\";", "language": "javascript" } ] } [/block] This code returns a JSON string. [block:code] { "codes": [ { "code": "response.body = JSON.stringify({error : \"Missing required fields : Name\"});", "language": "javascript" } ] } [/block] This code returns an error message. [block:code] { "codes": [ { "code": "if(request.body){\n try{\n JSON.parse(request.body).name;\n }catch(err){\n false;\n }\n}else{\n false;\n}", "language": "javascript" } ] } [/block] This code makes sure that if name isn't included in create then stop the execution, else move to Before request logic block. 3. The Before Request logic block has the following JavaScript code. [block:code] { "codes": [ { "code": "var body = JSON.parse(request.body);", "language": "javascript" } ] } [/block] This code grabs the request body. [block:code] { "codes": [ { "code": "if(!body.start_date){\n body.start_date = new Date().toLocaleString();\n}", "language": "javascript" } ] } [/block] This code checks if there is no start date, it sets start date to today. [block:code] { "codes": [ { "code": "if((body.status !== 'closed' && body.status !== 'open') || !body.status ){\n\tbody.status = \"open\";\n}", "language": "javascript" } ] } [/block] This code checks if the status is missing or not been set to either "open", or "closed", then sets it to "open". [block:code] { "codes": [ { "code": "if(body.end_date){\n body.endDate = body.end_date;\n delete body.end_date;\n}", "language": "javascript" } ] } [/block] This code changes underscore key to camel case key. [block:code] { "codes": [ { "code": "request.body = JSON.stringify(body);", "language": "javascript" } ] } [/block] This code adds manipulated body to the request body. 4. This Single Call component calls the Remote Endpoint “Event - POST” 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": "var response_data = JSON.parse(response.body).response.results;\nresponse.body = JSON.stringify(response_data);", "language": "javascript" } ] } [/block] This code drills down to the results object in the response. ## Step 6 Now let us test this Proxy Endpoint. Open a tool (such as Postman) where you can run a POST call to create a record. Run the following POST call with the body as shown below. [block:code] { "codes": [ { "code": "curl -H \"Content-Type: application/json\" -X POST -d '{\"name\": \"Event 10\",\"start_date\":\"2015-09-09\"}' http://lyrical-icicle-3650.justapis-staging.io/events", "language": "curl" } ] } [/block] You should see a message such as this. [block:code] { "codes": [ { "code": "{\"id\": 107,\"name\": \"Event 10\",\"start_date\": \"2015-09-09\",\"status\": \"open\"}", "language": "json" } ] } [/block] Congratulations, you have completed this tutorial.