SDKs overview

Black History Facts offers Software Development Kits (SDKs) and community-contributed libraries to facilitate programmatic access to its extensive database of historical information. These tools are designed to streamline the integration of Black History Facts data into various applications, ranging from educational platforms to research tools. SDKs handle the underlying HTTP requests, authentication, and data parsing, allowing developers to focus on application logic rather than API mechanics. The official SDKs are maintained directly by the Black History Facts team, while community libraries are developed and supported by the broader developer community.

Using an SDK can reduce development time and potential errors by providing idiomatic language constructs for interacting with the Black History Facts API. This approach aligns with common API integration practices, where SDKs serve as wrappers around RESTful or GraphQL endpoints, simplifying data retrieval and manipulation. For example, many APIs, such as the Google Maps JavaScript API, provide SDKs to manage complex interactions and present a simpler interface to developers.

Official SDKs by language

The Black History Facts project provides official SDKs for popular programming languages. These SDKs are actively maintained and are the recommended way to interact with the Black History Facts API. Each SDK is designed to offer a native development experience within its respective language ecosystem.

Language Package Name Install Command Example Maturity
Python blackhistoryfacts-py pip install blackhistoryfacts-py Stable
Node.js @blackhistoryfacts/js npm install @blackhistoryfacts/js Stable
Go github.com/blackhistoryfacts/go-sdk go get github.com/blackhistoryfacts/go-sdk Beta

These SDKs provide methods for common operations such as fetching facts by date, retrieving biographies of historical figures, and searching for events by keyword. The Python SDK, for instance, typically uses object-oriented patterns to represent API resources, allowing developers to interact with historical data as Python objects. Similarly, the Node.js SDK leverages asynchronous patterns common in JavaScript development, often returning Promises for API calls. The Go SDK follows Go's conventions for error handling and struct-based data representation.

Installation

Installation of the Black History Facts SDKs follows standard package management practices for each respective language. An API key is required for most operations and should be obtained from the Black History Facts developer dashboard. It is recommended to manage API keys securely, for example, by using environment variables rather than hardcoding them directly into source code, a practice also advised by services like Firebase for API key management.

Python

To install the Python SDK, use pip:

pip install blackhistoryfacts-py

After installation, you can import the library and initialize the client:

import os
from blackhistoryfacts import BlackHistoryFactsClient

api_key = os.environ.get("BHF_API_KEY")
client = BlackHistoryFactsClient(api_key=api_key)

Node.js

To install the Node.js SDK, use npm:

npm install @blackhistoryfacts/js

After installation, you can require or import the module and initialize the client:

const BlackHistoryFacts = require('@blackhistoryfacts/js');
// Or using ES modules:
// import BlackHistoryFacts from '@blackhistoryfacts/js';

const apiKey = process.env.BHF_API_KEY;
const client = new BlackHistoryFacts.Client(apiKey);

Go

To install the Go SDK, use go get:

go get github.com/blackhistoryfacts/go-sdk

After installation, you can import the package and initialize the client:

package main

import (
	"context"
	"fmt"
	"os"

	bhf "github.com/blackhistoryfacts/go-sdk"
)

func main() {
	apiKey := os.Getenv("BHF_API_KEY")
	if apiKey == "" {
		fmt.Println("BHF_API_KEY environment variable not set")
		return
	}

	client := bhf.NewClient(apiKey)
	// Client is now ready for use
}

Quickstart example

This section provides a quickstart example demonstrating how to fetch a historical fact for a specific date using the official Python SDK. Similar logic applies to Node.js and Go SDKs with their respective syntax and asynchronous patterns.

Python Quickstart: Fetching a fact by date

This example retrieves a fact associated with January 1st from the Black History Facts database.

import os
from blackhistoryfacts import BlackHistoryFactsClient
from blackhistoryfacts.models import Fact

# Ensure your API key is set as an environment variable
# For example: export BHF_API_KEY="your_api_key_here"
api_key = os.environ.get("BHF_API_KEY")

if not api_key:
    print("Error: BHF_API_KEY environment variable not set.")
    exit(1)

try:
    client = BlackHistoryFactsClient(api_key=api_key)
    
    # Fetch a fact for January 1st
    date_str = "01-01"
    print(f"Fetching fact for date: {date_str}")
    fact: Fact = client.facts.get_by_date(date_str)
    
    if fact:
        print("\n--- Fact Retrieved ---")
        print(f"Date: {fact.date}")
        print(f"Event: {fact.event}")
        print(f"Category: {fact.category}")
        if fact.source_url:
            print(f"Source: {fact.source_url}")
        print("----------------------")
    else:
        print(f"No fact found for {date_str}.")
	except Exception as e:
    print(f"An error occurred: {e}")

This Python script initializes the client with an API key from environment variables, then calls the get_by_date method to retrieve a Fact object. The script then prints the details of the retrieved fact or a message if no fact is found for the specified date.

Node.js Quickstart: Fetching a fact by date

const BlackHistoryFacts = require('@blackhistoryfacts/js');

const apiKey = process.env.BHF_API_KEY;

if (!apiKey) {
    console.error("Error: BHF_API_KEY environment variable not set.");
    process.exit(1);
}

async function getFactByDate() {
    try {
        const client = new BlackHistoryFacts.Client(apiKey);
        const dateStr = "01-01";
        console.log(`Fetching fact for date: ${dateStr}`);
        const fact = await client.facts.getByDate(dateStr);

        if (fact) {
            console.log("\n--- Fact Retrieved ---");
            console.log(`Date: ${fact.date}`);
            console.log(`Event: ${fact.event}`);
            console.log(`Category: ${fact.category}`);
            if (fact.sourceUrl) {
                console.log(`Source: ${fact.sourceUrl}`);
            }
            console.log("----------------------");
        } else {
            console.log(`No fact found for ${dateStr}.`);
        }
    } catch (error) {
        console.error(`An error occurred: ${error.message}`);
    }
}

getFactByDate();

Go Quickstart: Fetching a fact by date

package main

import (
	"context"
	"fmt"
	"log"
	"os"

	bhf "github.com/blackhistoryfacts/go-sdk"
)

func main() {
	apiKey := os.Getenv("BHF_API_KEY")
	if apiKey == "" {
		log.Fatal("BHF_API_KEY environment variable not set")
	}

	client := bhf.NewClient(apiKey)

	dateStr := "01-01"
	fmt.Printf("Fetching fact for date: %s\n", dateStr)

	ctx := context.Background()
	fact, err := client.Facts.GetByDate(ctx, dateStr)
	if err != nil {
		log.Fatalf("Error fetching fact: %v", err)
	}

	if fact != nil {
		fmt.Println("\n--- Fact Retrieved ---")
		fmt.Printf("Date: %s\n", fact.Date)
		fmt.Printf("Event: %s\n", fact.Event)
		fmt.Printf("Category: %s\n", fact.Category)
		if fact.SourceURL != "" {
			fmt.Printf("Source: %s\n", fact.SourceURL)
		}
		fmt.Println("----------------------")
	} else {
		fmt.Printf("No fact found for %s.\n", dateStr)
	}
}

Community libraries

In addition to the official SDKs, the Black History Facts developer community has contributed libraries in other programming languages. These libraries are typically open-source and maintained independently by their creators. While they may not offer the same level of official support as the SDKs, they can provide valuable alternatives for developers working in specific environments or preferring different language ecosystems.

Community libraries often emerge from developers' needs to integrate an API into a language not officially supported. For example, the Mozilla Developer Network documents various HTTP client libraries across languages, many of which are community-driven. Before using a community library, it's advisable to check its documentation, active maintenance status, and community support channels.

Notable community-contributed libraries include:

  • Java Client: A Java client library, blackhistoryfacts-java-client, is available on GitHub and Maven Central. It provides a set of classes for interacting with the Black History Facts API using Java's object-oriented paradigms. Installation typically involves adding a dependency to your pom.xml or build.gradle file.
  • Ruby Gem: A Ruby gem, black_history_facts_api, is available on RubyGems.org. It offers a Ruby-idiomatic interface for fetching historical data, often leveraging metaprogramming features common in Ruby development. Installation is done via gem install black_history_facts_api.

Developers are encouraged to review the source code and documentation of community libraries to ensure they meet their project's requirements and security standards. Contributions to these projects, such as bug fixes or feature enhancements, are typically welcomed by their maintainers.