Search-postcode-or-place returns 500

curl --location --request POST ‘https://api.nhs.uk/service-search/search-postcode-or-place?api-version=2&search=manchester’ --header ‘subscription-key: e065dbe14b074e918566188434ff7dda’

returns 500 with “optionalErrorName”: “We can’t find ‘manchester’”, …

Hello nsenkevich,
Can you help point me towards the service you are using?
Example: Find a GP - NHS (www.nhs.uk)? This particular one only allows 8 characters (Postcode) and does not support partial postcode entries.
Thank you

I’m trying to use this api endpoint with trial key see doc bellow

Hi nsenkevich,

Please could you try the following cURL command …

curl --location 'https://api.nhs.uk/service-search/search-postcode-or-place?api-version=2&search=Manchester' \
--header 'Content-Type: application/json' \
--header 'subscription-key: e065dbe14b074e918566188434ff7dda' \
--data '{
  "filter": "OrganisationTypeId eq '\''DEN'\''",
  "top": 10,
  "skip": 0,
  "count": true
}'

This will return the following JSON response …

{
  "place": [
    {
      "Latitude": 53.478941671902611,
      "Longitude": -2.245277998298477,
      "text": "Manchester, North West",
      "url": "https://api.nhs.uk/service-search/search-postcode-or-place?api-version=1&search=Manchester&latitude=53.4789416719026&longitude=-2.24527799829848"
    },
    {
      "Latitude": 53.514265448236706,
      "Longitude": -2.4199003923191209,
      "text": "New Manchester, North West",
      "url": "https://api.nhs.uk/service-search/search-postcode-or-place?api-version=1&search=Manchester&latitude=53.5142654482367&longitude=-2.41990039231912"
    }
  ]
}

This is an example of when the search parameter matches more than one entry. In this situation you would need to send a second request to the API using one of the suggested endpoints that includes the more specific latitude and longitude.

I hope that helps.

Hi Adrian,

curl --location --request POST ‘https://api.nhs.uk/service-search/search-postcode-or-place?api-version=2&search=manchester
–header ‘Content-type: application/json’
–header ‘subscription-key: 0407c65368f5447aa3a4d2469e49effd’

What are we doing wrong here?

Hi Camran,

The following Service Search API v2 example tries to find GP practices (GPB) in ‘Manchester’.

curl --location 'https://api.nhs.uk/service-search/search-postcode-or-place?api-version=2&search=Manchester' \
--header 'Content-Type: application/json' \
--header 'subscription-key: YOUR_SUBSCRIPTION_KEY_GOES_HERE' \
--data '{
  "filter": "OrganisationTypeId eq '\''GPB'\''",
  "top": 50,
  "skip": 0,
  "count": true
}'

However, when it tries to match ‘Manchester’ two different places are found …

  • Manchester, North West
  • New Manchester, North West
{
  "place": [
    {
      "Latitude": 53.478941671902611,
      "Longitude": -2.245277998298477,
      "text": "Manchester, North West",
      "url": "https://api.nhs.uk/service-search/search-postcode-or-place?api-version=1&search=Manchester&latitude=53.4789416719026&longitude=-2.24527799829848"
    },
    {
      "Latitude": 53.514265448236706,
      "Longitude": -2.4199003923191209,
      "text": "New Manchester, North West",
      "url": "https://api.nhs.uk/service-search/search-postcode-or-place?api-version=1&search=Manchester&latitude=53.5142654482367&longitude=-2.41990039231912"
    }
  ]
}

When multiple places are returned like this you should let the user choose which one they meant, and then use the longitude and latitude co-ordinates in a second API call like so …

curl --location --request POST 'https://api.nhs.uk/service-search/search?api-version=2' \
  --header 'Content-Type: application/json' \
  --header 'subscription-key: YOUR_SUBSCRIPTION_KEY_GOES_HERE' \
  --data-raw '{
  "filter": "OrganisationTypeId eq '\''GPB'\''",
  "orderby": "geo.distance(Geocode, geography'\''POINT(-2.24527799829848 53.4789416719026)'\'')",
  "top": 50,
  "skip": 0,
  "count": true
}'

Let me know if you require the example above in a specific programming language.
This interactive guide may help you.

Many thanks for this, Adrian.

Is it possible to give us a Postman export, as I am struggling to add the filter stuff in Postman to see the query?

Do you have a PHP example of the code by any chance?

Many thanks once again.

Adrian, just another one, I have tried different areas such as “telford”, “birmmingham”, and “wolverhampton”, but I am getting a “we can’t find” error.

You’re a star, just saw this, many thanks.

Hi Camran,

Here is a small Postman collection you can import …

{
  "info": {
    "_postman_id": "61886a8a-cd0b-4cf1-89a3-93434900b0e4",
    "name": "Service Search API (version 2)",
    "schema": "https://schema.getpostman.com/json/collection/v2.0.0/collection.json",
    "_exporter_id": "222854"
  },
  "item": [
    {
      "name": "Search by Place (Manchester)",
      "request": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json"
          },
          {
            "key": "subscription-key",
            "value": "0407c65368f5447aa3a4d2469e49effd"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n  \"filter\": \"OrganisationTypeId eq 'GPB'\",\n  \"select\": \"\",\n  \"top\": 50,\n  \"skip\": 0,\n  \"count\": true\n}"
        },
        "url": {
          "raw": "{{baseURL1}}/service-search/search-postcode-or-place?api-version=2&search=Manchester",
          "host": [
            "{{baseURL1}}"
          ],
          "path": [
            "service-search",
            "search-postcode-or-place"
          ],
          "query": [
            {
              "key": "api-version",
              "value": "2"
            },
            {
              "key": "search",
              "value": "Manchester"
            }
          ]
        }
      },
      "response": []
    },
    {
      "name": "Search by Geographic Co-ordinates (-2.24527799829848 53.4789416719026)",
      "request": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json"
          },
          {
            "key": "subscription-key",
            "value": "0407c65368f5447aa3a4d2469e49effd"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n  \"filter\": \"OrganisationTypeId eq 'GPB'\",\n  \"orderby\": \"geo.distance(Geocode, geography'POINT(-2.24527799829848 53.4789416719026)')\",\n  \"select\": \"\",\n  \"top\": 10,\n  \"skip\": 0,\n  \"count\": true\n}"
        },
        "url": {
          "raw": "{{baseURL1}}/service-search/search?api-version=2",
          "host": [
            "{{baseURL1}}"
          ],
          "path": [
            "service-search",
            "search"
          ],
          "query": [
            {
              "key": "api-version",
              "value": "2"
            }
          ]
        }
      },
      "response": []
    }
  ]
}

Here is some PHP code to search for GP practices (GPB) in ‘Manchester’ …

<?php

$url = 'https://api.nhs.uk/service-search/search-postcode-or-place?api-version=2&search=Manchester';

$headers = [
    'Content-Type: application/json',
    'subscription-key: 0407c65368f5447aa3a4d2469e49effd'
];

$data = [
    "filter" => "OrganisationTypeId eq 'GPB'",
    "select" => "",
    "top" => 50,
    "skip" => 0,
    "count" => true
];

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));

$response = curl_exec($ch);

if ($response === false) {
    $error = curl_error($ch);
    curl_close($ch);
    die('Curl error: ' . $error);
}

curl_close($ch);

echo $response;

?>

Here is some PHP code to search for GP practices (GPB) near the geographic co-ordinates (Longitude and Latitude) -2.24527799829848 53.4789416719026 …

<?php

$url = 'https://api.nhs.uk/service-search/search?api-version=2';

$headers = [
    'Content-Type: application/json',
    'subscription-key: 0407c65368f5447aa3a4d2469e49effd'
];

$data = [
    "filter" => "OrganisationTypeId eq 'GPB'",
    "orderby" => "geo.distance(Geocode, geography'POINT(-2.24527799829848 53.4789416719026)')",
    "select" => "",
    "top" => 50,
    "skip" => 0,
    "count" => true
];

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));

$response = curl_exec($ch);

if ($response === false) {
    $error = curl_error($ch);
    curl_close($ch);
    die('Curl error: ' . $error);
}

curl_close($ch);

echo $response;

?>

You can find a list of other OrganisationTypeId’s here …

Many thanks for this, it truly means a lot.

Yayyy, its working now.

A quick question, does this service by default check for X miles radius / nearby?

Hi Camran,

No, I’m afraid the distance isn’t included in the response, however the results are ordered by distance.

Once you get the response you could loop through each value in the array and use the Haversine formula to calculate the distance, and then store that in each object. There are various implementations of the Haversine formula in different programming languages available online.

Here is an example I’ve knocked up in PHP …

<?php
function getDistanceBetweenGeoCoordinates($coord1, $coord2) {
    $toRadians = function($degrees) {
        return $degrees * M_PI / 180;
    };

    $R = 3958.8; // Radius of the Earth in miles
    $lat1 = $toRadians($coord1['latitude']);
    $lon1 = $toRadians($coord1['longitude']);
    $lat2 = $toRadians($coord2['latitude']);
    $lon2 = $toRadians($coord2['longitude']);

    $dlat = $lat2 - $lat1;
    $dlon = $lon2 - $lon1;

    $a = sin($dlat / 2) ** 2 + cos($lat1) * cos($lat2) * sin($dlon / 2) ** 2;
    $c = 2 * atan2(sqrt($a), sqrt(1 - $a));

    return $R * $c;
}

$geoCoordinates1 = array(
    'longitude' => -2.24527799829848,
    'latitude' => 53.4789416719026
);

$geoCoordinates2 = array(
    'longitude' => -1.9291420317918668,
    'latitude' => 50.7265664628282
);

$distanceMiles = getDistanceBetweenGeoCoordinates($geoCoordinates1, $geoCoordinates2);

echo 'distanceMiles: ' . $distanceMiles;
?>
1 Like

Thank you for this, I will check it out.

Apologies, for the late response, as a new member It wouldn’t allow to message you yesterday.

I requested full access to the API and received an email with some questions saying, to reply to this email.

After sending the email, I got an email to say, “Message not delivered”.

Is it possible to look into this for us, please?

Many thanks.
Cam