# Creating API keys **Category:** [Growth Charts](https://forum.rcpch.tech/c/rcpch-digital-growth-charts/6) **Created:** 2021-03-18 15:18 UTC **Views:** 1103 **Replies:** 12 **URL:** https://forum.rcpch.tech/t/creating-api-keys/35 --- ## Post #1 by @raman0503 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. --- ## Post #2 by @pacharanero 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](https://www.postman.com/) initially. We have created a ready-made testing collection for Postman, which is available [here](https://app.getpostman.com/run-collection/6b1137d60067b8aedfea#?env%5Blocalhost%3A5000-testing%5D=W3sia2V5IjoiYmFzZVVybCIsInZhbHVlIjoibG9jYWxob3N0OjUwMDAiLCJlbmFibGVkIjp0cnVlfV0=) 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/ 1. Select the Free Tier https://dev.rcpch.ac.uk/product#product=starter 1. Click on the big blue 'Subscribe' button 1. Create an 'application' which holds your API keys 1. you should be able to get your API keys in https://dev.rcpch.ac.uk/profile ### POSTing * Include a Header of `Primary-Subscription-Key` which contains your Primary Key from your API account * POST a Body such as ```json { "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. --- ## Post #3 by @raman0503 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! --- ## Post #4 by @pacharanero 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 --- ## Post #5 by @raman0503 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 --- ## Post #6 by @pacharanero 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 ########### 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)) ``` --- ## Post #7 by @raman0503 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 --- ## Post #8 by @raman0503 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: ```python 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) --- ## Post #9 by @pacharanero @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. https://github.com/rcpch/digital-growth-charts-server/pull/145 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' ``` --- ## Post #10 by @raman0503 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. --- ## Post #11 by @pacharanero 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. --- ## Post #12 by @pacharanero --- ## Post #13 by @pacharanero --- **Canonical:** https://forum.rcpch.tech/t/creating-api-keys/35 **Original content:** https://forum.rcpch.tech/t/creating-api-keys/35