Blog

How to integrate an API in my Healthcare software from Nubentos

by | 5 Oct 2019 | 0 comments

by | Oct 5, 2019 | Howtos | 0 comments

Software developers for Health, interested in using an API available in the Nubentos API Store, you should know the steps to integrate an API that interests you in your developments.

In this article we explain the necessary steps to be able to use a Digital Health API in your software developments. Are you ready to impress your users?

First steps in the Nubentos API Store to integrate an API

Firstly, you will have to manage access to the API. Once registered as a user you can complete the subscription process.

It’s as simple as creating an Application and subscribing to the API (Subscription) by choosing the plan (Subscription tier) that best suits your needs and budget.

Secondly, you can test the API directly in our Integrated Test Console, understand how it works and what information it returns.

Then, it’s time to integrate it into your software. To do this you need to know what steps are necessary to connect to the API from your source code, and have access to the functionality it provides. 

At this point, the role played by the access token to access an API is key, and you should know how to use it to be able to interact with an API from your code.

How to obtain the access token

Associated with the application that you have used to subscribe to the API, the platform will have generated a “consumer-key” and a “consumer-secret” that represent the credentials of the application.

Consumer Key Secret nubentos

 

With these two values ​​you can request the access token that you will use in the API requests.

To do this from your software you must make a request to the URL https://apigw.nubentos.com/token, passing as the authorization header the result of the function “base64 (consumer-key: consumer-secret)”.

 

An example of the code needed in Python:

 

import requests

import base64

import json

 

token_url = ‘https: //apigw.nubentos.com/token’

key = ‘Basic% s’% base64.b64encode (b’2fU ****** **************** Q8a: u7N ********************** tsa ‘). Decode (‘ ascii ‘ )

data = {‘grant_type’: ‘client_credentials’}

headers = {‘Authorization’: key, ‘Content-Type’: ‘application / x-www-form-urlencoded’}

 

r = requests.post (token_url, data = data , headers = headers)

 

rs = r.content.decode () 

response = json.loads (rs)

token = response [‘access_token’]

 

print (token)

 

The response when executing the code will be the access token and it will have the following format : 

 

557 ***** – **** – ********* – ********* b40

 

With this simple step you already have the access token for the API you want to consume.

In the next point we will use a test API (MedicalAPI) and we will use the SDK provided by the platform. 

About MedicalAPI 

This API is published in the Nubentos API Store. It is a free API demo, so that developers can interact with the Nubentos platform and familiarize themselves with the entire subscription, test and integration process.

It provides very basic resources and methods to manage a clinic: patient data, doctors and the inventory of material and supplies.

This API is intended to return self-generated structured data, so that no real information is used.

To access this API and follow the following examples, the steps to follow are: 

  • Sign up in the private API store “nubentos”:

https://apimarketpre.nubentos.com/store/?tenant=nubentos.com

  • Create an Application and generate the access tokens for the Production / Sandbox environment
  • Subscribe to the selected API Free Level.

Once you have access to the API you can interact with the API Test Console to see how the different resources work. 

How to consume the API from your Python code

The easiest way is to integrate the code that provides the API SDKs.

You can download it from the “SDKs” tab, where you have up to 10 different ones for the most common development technologies. 

SDKs nubentos

 

The file downloaded in .zip format has the following content structure:

capture nubentos

 

We can see that we have a README.md file that describes how to use its contents and references to the “docs” folder where there is documentation of the methods and procedures to make use of the API.

Also included is the “requirements.txt” file, commonly used to install the python dependencies to be able to use the code and that we must add to the requirements of our project.

If for example we want to use the GET method for the patient resource (patient) by patient name (patientname), in the API Console we can see that it only requires a parameter of type string and returns us a response in JSON format with fields of the records that match.

Patient by PatientName nubentos

 

If we see the documentation file ./docs/PatientApi.md, we can identify that there is a method called “getpatient_by_name” that invokes this API call, and the code proposed to make the call. 

import time

import swagger_client

from swagger_client.rest import ApiException

from pprint import pprint

 

# Configure OAuth2 access token for authorization: default

swagger_client.configuration.access_token = ‘YOUR_ACCESS_TOKEN’

 

# create an instance of the API class

api_instance = swagger_client.PatientApi ()

patientname = ‘patientname_example’ # str | The name that needs to be fetched. Use patient1 for testing. 

 

try: 

    # Get patient by patient name

    api_response = api_instance.getpatient_by_name (patientname)

    pprint (api_response)

except ApiException as e:

    print “Exception when calling PatientApi-> getpatient_by_name:% s \ n”% e

 

In this code we have to indicate the access token in the following line:

 

# Configure OAuth2 access token for authorization: default

swagger_client.configuration.access_token = ‘557 ***** – **** – ********* – ***** **** b40 ‘

 

We had already obtained this token previously.

Also, we have to indicate the endpoint to which we are going to make the request. To do this we add a line following the one indicated above and also take the opportunity to indicate the name of the patient that we want to identify by changing the value of the variable “patientname”.

Our code will look like this:

 

import time

import swagger_client

from swagger_client.rest import ApiException

from pprint import pprint

 

# Configure OAuth2 access token for authorization

swagger_client.configuration.access_token = ‘557 ***** – **** – ***** **** – ********* b40 ‘

 

swagger_client.configuration.host = “https://apigw.nubentos.com:443/t/nubentos.com/medapp/1.0.0

 

# create an instance of the API class

api_instance = swagger_client.PatientApi ()

 

patientname = ‘antonio’ ​​# str | The name that needs to be fetched.

 

try: 

    # Get patient by patient name

    api_response = api_instance.getpatient_by_name (patientname)

    pprint (api_response)

except ApiException as e:

    print “Exception when calling PatientApi-> getpatient_by_name:% s \ n”% e

With this code saved in a file with extension “.py” (in this case called fetch_patient.py) we could get the following result:

 

$ python ./fetch_patient.py      

                                                                                                                                          

{’email’: ‘magna non labore incididunt id’,

 ‘first_name’: ‘incididunt’,

 ‘id’: -63222291,

 ‘last_name’: ‘sit qui commodo’,

 ‘password’: ‘ex non’,

 ‘patient_status’ : -14293061,

 ‘patientname’: ‘Duis culpa proident consequat officia’,

 ‘phone’: ‘Excepteur commodo’}

 

As we said before, the data returned by this API is simply for testing. In this example we have used the SDK generated for Python and we have executed it with Python 2.7, but there are SDKs for the main programming languages.

Conclusions on how to integrate an API

As we have seen in this article, using a few lines of code and using the SDKs generated by the Nubentos platform, it is possible to integrate an API easily, saving us a lot of time in implementing all calls to the different resources and methods of the API.

Your development time to have the integration ready is also much less than in a traditional integration, because you have been able to test the API thoroughly in the Integrated Test Console in the Nubentos API Store, without writing a line of code.

Thanks to this, your code will have fewer errors and your testing phase will be much shorter.

Now you can delight your users with the advanced Digital Health solutions that we are constantly incorporating into the Nubentos API Store.

If you want to receive all the news about our platform each month, do not hesitate to subscribe to our newsletter.

And if you have already seen an API that you want to use in your software products, you already know the steps you should follow.

Thank you for reading this article and happy integration!

 

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Your competitors know, don’t be left out.

Receive in your mail all the news about Nubentos: articles, eBooks, new APIs, interviews, guides, etc. in our Newsletter with the best of each month.