SDKs overview

The openFDA API provides access to a range of public health datasets, including drug adverse events, medical device reports, and food recalls. While openFDA primarily serves its data via a RESTful API without requiring an API key, simplifying direct HTTP requests, official and community-contributed Software Development Kits (SDKs) and libraries exist to streamline interaction for specific programming environments. These tools abstract the underlying HTTP requests, handle JSON parsing, and present a more idiomatic interface for developers working in languages like Python and R. The goal of these SDKs is to reduce boilerplate code and accelerate the development of applications that consume openFDA data.

Developers can use these SDKs to query specific datasets, filter results, and process the structured data returned by the API. The openFDA API's design, which emphasizes JSON as its output format, allows for straightforward integration with client-side applications and data analysis scripts. For instance, a developer might use a Python library to fetch drug adverse event reports, then integrate that data into a local database or a data science workflow using libraries like Pandas or NumPy. Similarly, R packages facilitate statistical analysis directly within the R environment, connecting openFDA data with established epidemiological and biostatistical methods.

Official SDKs by language

openFDA supports official client libraries primarily for Python and R, reflecting the API's strong use case in data science, research, and analytics. These libraries are maintained to ensure compatibility with the API's evolving structure and provide a consistent interface for developers.

Official openFDA SDKs
Language Package Name Install Command Maturity
Python fda pip install fda Stable
R openfda install.packages("openfda") Stable

These official SDKs are designed to abstract the complexities of HTTP requests, URL construction, and JSON parsing, allowing developers to focus on data utilization. For detailed documentation on specific methods and functionalities, refer to the openFDA API documentation.

Installation

Before you begin, ensure you have the appropriate package manager installed for your chosen language (pip for Python, R's built-in package manager for R). No API key is required to use the openFDA API, which means you can start making requests immediately after installing the SDK.

Python

To install the official Python SDK, fda, use pip:

pip install fda

After installation, you can verify it by importing the library in a Python interpreter:


import fda
print(fda.__version__)

This command installs the latest stable version of the fda package. For more details on the Python SDK, including its modules and functions, consult the openFDA Python SDK documentation.

R

To install the official R SDK, openfda, use R's built-in install.packages() function:

install.packages("openfda")

Once installed, load the library into your R session:


library(openfda)

You can then begin making API calls. For comprehensive information on the R package's capabilities and examples, refer to the openFDA R package documentation.

Quickstart example

These quickstart examples demonstrate how to fetch data using the official Python and R SDKs. Both examples will query the drug adverse events dataset.

Python Quickstart

This Python example fetches the first 10 drug adverse event reports where the patient's age is greater than 60.


import fda

# Initialize the client
client = fda.client()

# Define the search parameters
search_term = "patient.patientratiomeddra.patientage > 60"
limit = 10

# Query the drug adverse events endpoint
results = client.drug.event.search(search_term, limit=limit)

# Print the results
if results and 'results' in results:
    print(f"Found {len(results['results'])} drug adverse events:")
    for event in results['results']:
        safety_report_id = event.get('safetyreportid', 'N/A')
        patient_age = event.get('patient', {}).get('patientratiomeddra', {}).get('patientage', 'N/A')
        print(f"  Safety Report ID: {safety_report_id}, Patient Age: {patient_age}")
else:
    print("No results found or an error occurred.")

R Quickstart

This R example fetches the first 10 drug adverse event reports where the patient's age is greater than 60.


library(openfda)

# Define the search parameters
search_term <- "patient.patientratiomeddra.patientage:[60 TO *]"
limit_val <- 10

# Query the drug adverse events endpoint
# The openfda R package uses a slightly different syntax for search queries
drug_events <- fda_query("drug/event") \
  %>% fda_search(search_term) \
  %>% fda_limit(limit_val)

# Fetch and print the results
results <- fda_fetch(drug_events)

if (length(results$results) > 0) {
  cat(paste0("Found ", length(results$results), " drug adverse events:\n"))
  for (i in seq_along(results$results)) {
    event <- results$results[[i]]
    safety_report_id <- ifelse(!is.null(event$safetyreportid), event$safetyreportid, "N/A")
    patient_age <- ifelse(!is.null(event$patient$patientratiomeddra$patientage), event$patient$patientratiomeddra$patientage, "N/A")
    cat(paste0("  Safety Report ID: ", safety_report_id, ", Patient Age: ", patient_age, "\n"))
  }
} else {
  cat("No results found or an error occurred.\n")
}

Community libraries

Beyond the official SDKs, the open-source community has developed various libraries and tools to interact with the openFDA API. These community-driven projects often cater to specific use cases, integrate with other popular frameworks, or offer alternative interfaces. While not officially maintained by the FDA, they can provide valuable utilities for developers.

Examples of community contributions often include wrappers for languages not officially supported, data visualization tools built on openFDA data, or command-line interfaces for quick data extraction. Developers should evaluate these libraries based on their project's needs, maintenance activity, and community support.

  • JavaScript/Node.js Libraries: While no universally recognized official or major community SDK exists, developers often use standard HTTP client libraries like axios or node-fetch directly to interact with the web's Fetch API for JavaScript. This direct approach offers flexibility and control, allowing developers to build custom wrappers or integrate data into web applications. For example, a simple fetch call can retrieve drug label information.
  • Other Languages: For languages like Java, C#, or Go, developers typically construct HTTP requests directly to the RESTful endpoints and parse the JSON responses using native language features or popular JSON parsing libraries. This approach is common when an official SDK is not available, as outlined in general API integration best practices from organizations like the W3C for web standards.

When considering community libraries, it is advisable to check the project's documentation, GitHub repository activity, and issue tracker to assess its reliability and ongoing support. Feedback and contributions to these projects are often welcomed by their maintainers, fostering a collaborative ecosystem around openFDA data.