U.S. flag

An official website of the United States government

Access the API: R Software

There are multiple options for R packages that can send API requests. This example shows how the httr and jsonlite packages can be used to reach the DSID API. To download the full R Code shown in this page, use this link: (dsid_api_R_example_code.R)

Step 1: Load packages

Create a new R Script (File → New File → R Script) to type and enter the code for this API Request. First, install and load the httr and jsonlite packages.

DSID API Example Response
DSID API Example Response

Step 2: Build input fields

Example goal: Estimate calcium levels in adult MVMs using a label amount of 200mg. In your script, create a data frame object that contains the input values.

DSID API Example Response

This “input_data” object now contains the required input fields to pass to the API. Type input_data into the console to see its value.

DSID API Example Response

Step 3: Convert input to JSON format

This input list can be converted to JSON format using the toJSON function from the jsonlite package.

DSID API Example Response

Step 4: Send a POST request

Using your JSON input data and the DSID API URL, you can now send a request with the POST function from the httr package. Be sure to include the additional header functions accept_json() and content_type_json() – these allow the API to know what type of data to expect.

DSID API Example Response

If the request was formatted and went through properly, you should see a successful status code of 200.

DSID API Example Response

Step 5: View and save output

To view the result of the API request, use the httr content function and specify to return it as text. Use cat to print the JSON results; to save the result to a JSON file, use the file argument to provide the path to a .json file.

DSID API Example Response

To convert the JSON data back to a data frame, you can use the fromJSON function.

DSID API Example Response

You can continue to work with the results in R or import them into other software. See the Converting JSON Data page for an Excel example.