Example Call to Access Public Data

The following is an example call using curl to access public data using an API from the NBIA v1 API Directory.

To come

Example Request for Accessing a Private Resource

Most data in TCIA are fully public. However, certain collections require special permissions to access them. To access these collections, you must first register for a TCIA account (be sure to scroll down on this page and click "CLICKING HERE" to accept the terms and conditions) and then contact the TCIA Help Desk for permission to access the restricted data. The TCIA Help Desk will also give you the client_id and client_secret. An example follows that shows how to request NBIA REST services for restricted data.


import requests,json

token_url = "https://services.cancerimagingarchive.net/nbia-api/oauth/token?username=YOUR_NAME&password=YOUR_PASSWORD&grant_type=password&client_id=nbiaRestAPIClient&client_secret=ItsBetweenUAndMe"

access_token = requests.get(token_url).json()["access_token"]

print (access_token)


Python Sample Output:

ae8b76ac-4225-4958-8eba-27ddd4b15411

Using the Token in an API Call

Make a note of the access token you received and pass it with the REST service call.

import requests,json

token_url = "https://services.cancerimagingarchive.net/nbia-api/oauth/token?username=YOUR_NAME&password=YOUR_PASSWORD&grant_type=password&client_id=nbiaRestAPIClient&client_secret=ItsBetweenUAndMe"
access_token = requests.get(token_url).json()["access_token"]
print (access_token)

api_call_headers = {'Authorization': 'Bearer ' + access_token}
data_url = "https:// services.cancerimagingarchive.net/nbia-api/services/v2/getModalityValues"
data = requests.get(data_url, headers=api_call_headers).json()
print (data)

Python Sample Output

[{"Modality":"CR"},{"Modality":"CT"},{"Modality":"CTPT"},{"Modality":"DX"},{"Modality":"FUSION"},{"Modality":"KO"},{"Modality":"MG"},{"Modality":"MR"},{"Modality":"NM"},{"Modality":"OT"},{"Modality":"PR"},{"Modality":"PT"},{"Modality":"REG"},{"Modality":"RTDOSE"},{"Modality":"RTPLAN"},{"Modality":"RTSTRUCT"},{"Modality":"RWV"},{"Modality":"SC"},{"Modality":"SEG"},{"Modality":"SR"},{"Modality":"US"},{"Modality":"XA"}]

Example Request for Logout


import requests,json

token_url = "https://services.cancerimagingarchive.net/nbia-api/oauth/token?username=YOUR_NAME&password=YOUR_PASSWORD&grant_type=password&client_id=nbiaRestAPIClient&client_secret=ItsBetweenUAndMe"
access_token = requests.get(token_url).json()["access_token"]
print (access_token)

api_call_headers = {'Authorization': 'Bearer ' + access_token}
data_url = "https:// services.cancerimagingarchive.net/nbia-api/logout "
data = requests.get(data_url, headers=api_call_headers).json()
print (data)

Python Sample Result

You Have Logged Out successfully.

Sample Python Script for REST Search APIs

Body Part Values and Counts API

The Body Part Values and Counts API returns the modality values and body part count for the modality. It optionally takes the following parameters.

Example Body Part Values and Counts Query

import requests,json

token_url = "https://services.cancerimagingarchive.net/nbia-api/oauth/token?username=YOUR_NAME&password=YOUR_PASSWORD&grant_type=password&client_id=nbiaRestAPIClient&client_secret=ItsBetweenUAndMe"
access_token = requests.get(token_url).json()["access_token"]
print (access_token)

api_call_headers = {'Authorization': 'Bearer ' + access_token}
data_url = "https:// services.cancerimagingarchive.net/nbia-api/services/getBodyPartValuesAndCounts?Modality=PT "
data = requests.get(data_url, headers=api_call_headers).json()
print (data)


Python Sample Results

[{"criteria":"ABDOMEN","count":"145"},{"criteria":"BLADDER","count":"6"},{"criteria":"BRAIN","count":"42"},{"criteria":"BREAST","count":"125"},{"criteria":"CHEST","count":"232"},{"criteria":"EXTREMITY","count":"51"},{"criteria":"HEADNECK","count":"395"},{"criteria":"KIDNEY","count":"1"},{"criteria":"LIVER","count":"1"},{"criteria":"LUNG","count":"40"},{"criteria":"PHANTOM","count":"22"},{"criteria":"PROSTATE","count":"12"},{"criteria":"THORAX_1HEAD_NE","count":"4"},{"criteria":"THYROID","count":"1"},{"criteria":"UNDEFINED","count":"2"},{"criteria":"UTERUS","count":"5"},{"criteria":"WHOLEBODY","count":"5"}]