Getting Started

Use Chrome Advanced REST Client to try out NHS Data REST API

Google Chrome

Use Google Chrome's REST client.

Open this window in Chrome and click here. Click the install button to install the REST Client. Alternatively, from Chrome, using your favourite search engine enter the search term 'chrome rest client', the first item on the page should take you to the Chrome store where you can install the plug-in.

Download example C# code for getting data from NHS Data REST API

C# Code

Write your own client in C#.

Download Example

The minimum requirements for a C# application for requesting NHS data is shown below. The variable 'responseValue' will contain the JSON string representation of the requested data.

using System;
using System.Net;
using System.IO;

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://nhs-data.uk/api/1111/ods");
request.Method = "GET";
request.ContentLength = 0;
request.ContentType = "application/json";

HttpWebResponse response=(HttpWebResponse)request.GetResponse();
if (response.StatusCode!=HttpStatusCode.OK)
{
    throw new ApplicationException(String.Format("Request failed. Received HTTP {0}", response.StatusCode));
}

Stream responseStream = response.GetResponseStream();
if (responseStream != null)
{
    StreamReader reader = new StreamReader(responseStream);
    string responseValue=reader.ReadToEnd();
    Console.WriteLine(responseValue);
}
Download example JavaScript code using Angular for getting data from NHS Data REST API

JavaScript Code Using Angular.js

Write your own web client in JavaScript using Angular.js.

Download Example

The code below renders a text box, a submit button and a text area. On clicking submit data will be retreived and displayed in the text area. Simply copy this into a file with the extension .htm and open in a browser to try it out. Remember to replace [key] with the key you receive when you have registered.

<html>
 <body>
  <h1>NHS Data ReST Client Example</h1>
  <div ng-app="NHSDataRestClientExample" ng-controller="NHSDataController as ctrl">
   <input ng-model="ctrl.getUrl" style="width:100%" />
   <br />
   <button ng-click="ctrl.submitButtonClick()">Submit</button>
   <br />
   <textarea ng-model="ctrl.dataDisplay" style="width:100%;height:200px;"></textarea>
  </div>
  <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.js"></script>
  <script>
    angular
      .module("NHSDataRestClientExample", [])
      .controller("NHSDataController",NHSDataController);

    NHSDataController.$inject = ["$http"];

    function NHSDataController($http) {
      var vm = this;
      console.log("Started controller");

      vm.getUrl = "http://nhs-data.uk/api/[key]/ods/egpcur?Organisation Code=G0102";
      vm.dataDisplay = '';
      vm.submitButtonClick = submitButtonClick;

      function submitButtonClick() {
        vm.dataDisplay = "Getting data, please wait...";
        $http.get(vm.getUrl)
          .success(
            function (data) {
              console.log(JSON.stringify(data));
              // Get data for the first item returned only then convert the data to a JSON object.
              var JSONDataObj = JSON.parse(data[0].data);
              // Construct the datathat we will display in the textarea.
              vm.dataDisplay = 'Data:\n' + JSON.stringify(JSONDataObj);
              // Access specific attributes in the data.
              vm.dataDisplay += '\n\nName: ' + JSONDataObj.Name;
              vm.dataDisplay += '\nPostcode: ' + JSONDataObj.Name;
            }
          )
          .error(
            function (err) {
              console.error(JSON.stringify(err));
            }
          )
        }
     }
   </script>
 </body>
</html>
Example JavaScript code using Node.js for getting data from NHS Data REST API

JavaScript Code Using Node.js

Write your own server side client in JavaScript using Node.js.

The code snippet below shows how to retrieve data from a server side Node.js application. Remember to replace [key] with the key you receive when registered.

var http=require('http');

var uri="http://nhs-data.uk/api/[key]/ods/egpcur?Organisation Code=G0102";

http.get(uri,function(response){
  var responseString='';
  response.on(console.log('data',function(chunk){
    responseString+=chunk;
  });
  response.on(console.log('end',function(){
    // We've got the full response now convert to JSON object.
    var JSONObj=JSON.parse(responseString);
  });
}).on('error',function(e) {
    // Something went wrong.
})