Creating API keys

Hi Marcus,
Quick noob question. I am a poor Python coder and am struggling to generate a subscription key to try a post request. Is there a quick how to?
My intention is to generate pyplots using just the centile and see if that is translatable by a paediatrician, eventually moving to an integration with our EPMA which holds weight, height, dob and date of measurements as states.

1 Like

Hi @raman0503

There’s no such thing as a poor Python coder! Python coders are the best in the world!

I would always recommend to start with using an API testing tool like Postman initially. We have created a ready-made testing collection for Postman, which is available here which can be run on the web or in Postman’s native application.

Getting API keys

  1. Sign up at https://dev.rcpch.ac.uk/
  2. Select the Free Tier Products: Details - Digital Growth Charts API - Developer Portal
  3. Click on the big blue ‘Subscribe’ button
  4. Create an ‘application’ which holds your API keys
  5. you should be able to get your API keys in User: Profile - Digital Growth Charts API - Developer Portal

POSTing

  • Include a Header of Primary-Subscription-Key which contains your Primary Key from your API account
  • POST a Body such as
{
    "birth_date": "2020-04-12",
    "gestation_days": 4,
    "gestation_weeks": 40,
    "measurement_method": "height",
    "observation_date": "2020-06-12",
    "observation_value": 60,
    "sex": "male"
}

And it should work

If you have problems I’m happy to do a quick video call with you and get it working together.

Thanks Marcus, I was going blind and didn’t see the giant subscribe button. Seemed to work but I started getting 404 errors. I’ll keep on playing with it over the weekend. Thanks for the help!

404s? Are you sure you’re hitting the right URL?

Remember the developer portal (website where you register and get your API keys and manage subscriptions) is at dev.rcpch.ac.uk

But the API itself is at api.rcpch.ac.uk

(This separation of concerns is helpful over time as we upgrade and improve the dev portal and API, but I wondered if it might be confusing)

I am aware we do need a better ‘getting started’ guide. I’ll be working on it soon

Yep it was only briefly and the site itself was also down for about 10 mins last night. Not sure whether it was maintainance. It was fine after that. I am having a Json parsing error when I try and read and print the response but I think I can sort that out. A beginners guide would be great I think.

Dr Raman Sharma

1 Like

Ah OK, yes I did get an alert on UptimeRobot that it was briefly down. Not maintenance that I personally was doing, but within the hallowed halls of Azure they pretty much do what they want, it seems.

You can get code snippets for various languages in the developer portal. The fonts are a little hard to see because of a styling issue I’m trying to fix, but it is there. For example:

########### Python 2.7 #############
import httplib, urllib, base64

headers = {
# Request headers
'Content-Type': 'application/json',
'Cache-Control': 'no-cache',
'Primary-Subscription-Key': 'YOUR_API_KEY_HERE',
}

params = urllib.urlencode({
})

try:
conn.request("POST", "?%s" % params, headers)
    response = conn.getresponse()
    data = response.read()
    print(data)
    conn.close()
except Exception as e:
    print("[Errno {0}] {1}".format(e.errno, e.strerror))

That’s useful Marcus thanks. I was using print(response.json()) just to see an output which I think is the problem. I’ll give this a go.

Raman

Actually I am using Python 3 and still getting a 400 error from the post request. I can successfully get request but the post request is telling me something is off in my data request. I’ll keep on fiddling and post my code when it is successful.

Edit: Yay it was a silly error on my part. I had defined the parameters as params not json. Worked fine after that. Code below for Python 3:

import requests
import json
#headers to access the site
headers={
    "user-agent":"My_name",
    "Content-Type":"application/json",
    "Cache-Control":"no-cache",
    "Primary-Subscription-Key":"My_key",  
}  
#request parameters in a json dict format
parameters={
    "birth_date":"2014-07-04T00:00:00.0000000",
    "gestation_days":4,
    "gestation_weeks":40,
    "measurement_method":"height",
    "observation_date":"2021-03-01T00:00:00.0000000",
    "observation_value":123,
    "sex":"male",
}
#the post request
response=requests.post("https://api.rcpch.ac.uk/growth/v1/uk-who/calculation",headers=headers,json=parameters)
print(response.status_code)
#function to make it print in a readable format
def jprint(obj):
    text = json.dumps(obj, sort_keys=True, indent=4)
    print(text)
jprint(response.json())

(edited by @pacharanero to format code)

@raman0503 just as a side-note, I pushed code to the live server today which requires dates in a slightly different format - this is to align with FHIR and DPCHR specs.

Just to let you know because I can see from the logs you are using the shorter dates, so from today you’ll need to add the time as well. Yes, we know that time of day will have no effect on the centiles, and the server’s internal centile maths completely disregards time. But for now this was the most consistent way to proceed.

Use a datetime like this:

birth_date: '2020-04-12T00:00:00'

lol that really threw me for about an hour as the format is now YYYY-MM-DD. I was still requesting DD-MM-YYYY with the timestamp automatically catenated.

1 Like

If you are using Python then the datetime python package works very well in converting between standard string representations of datetime and the python Object. There is also dateutils which is even better, if you don’t mind having a third party package in there.