SDKs overview

Currents provides client-side Software Development Kits (SDKs) and libraries to facilitate integration with its News API. These tools are designed to simplify common tasks such as making API requests, handling authentication, and parsing JSON responses. By using an SDK, developers can reduce the amount of boilerplate code required and accelerate the development process, focusing on the specific application features rather than the underlying API communication protocols.

The SDKs are typically language-specific wrappers around the API's HTTP endpoints, offering idiomatic interfaces for each programming environment. For instance, a method in a Python SDK might correspond to a specific API endpoint, returning data as native Python objects (e.g., dictionaries or custom classes) instead of raw JSON strings. This approach enhances developer experience by aligning with familiar programming patterns and error handling mechanisms within each language ecosystem.

Currents primarily offers official SDKs for widely adopted programming languages, ensuring direct support and reliability for core integrations. While official SDKs are the recommended path for stability and feature completeness, the open nature of APIs also allows for community-driven libraries. These community contributions can sometimes offer specialized functionalities or support for niche languages not covered by official releases, though their maintenance and feature parity may vary.

Official SDKs by language

Currents offers official SDKs for several popular programming languages, ensuring that developers can integrate the News API efficiently within their preferred development environments. These SDKs are maintained by the Currents team and are designed to provide a consistent and reliable interface for accessing API functionalities. Each SDK typically includes methods for searching news articles, filtering results by various criteria (e.g., language, category, date), and retrieving specific article details.

The following table outlines the official SDKs available, including their respective package names, common installation commands, and general maturity status. Developers are encouraged to consult the Currents API reference documentation for the most up-to-date information on SDK versions, specific method signatures, and usage guidelines.

Language Package Name Install Command Maturity
Node.js @currentsapi/currents-api npm install @currentsapi/currents-api or yarn add @currentsapi/currents-api Stable
Python currentsapi-python pip install currentsapi-python Stable
PHP currentsapi/currents-php composer require currentsapi/currents-php Stable
Ruby currentsapi-ruby gem install currentsapi-ruby Stable
Go github.com/currentsapi/currents-go go get github.com/currentsapi/currents-go Stable

Each SDK is designed to encapsulate the API's authentication mechanism, typically requiring an API key for request authorization. Developers can obtain and manage their API keys through the Currents dashboard. The SDKs handle the secure transmission of these keys, often by including them in request headers or as query parameters, adhering to standard API security practices. This simplifies the authentication process, abstracting away the specifics of HTTP authentication headers, which is a common pattern in API client libraries as noted in the Google Cloud API key authentication documentation.

Installation

Installation of Currents SDKs follows the standard package management practices for each respective programming language. Below are detailed instructions for installing the official SDKs.

Node.js

For Node.js projects, the SDK is available via npm or Yarn. Navigate to your project directory in the terminal and execute one of the following commands:

npm install @currentsapi/currents-api

or

yarn add @currentsapi/currents-api

After installation, you can import the module into your JavaScript or TypeScript files:

const CurrentsAPI = require('@currentsapi/currents-api');
// Or for ES Modules:
// import CurrentsAPI from '@currentsapi/currents-api';

Python

The Python SDK can be installed using pip, Python's package installer. Open your terminal and run:

pip install currentsapi-python

Once installed, you can import the library into your Python scripts:

from currentsapi import CurrentsAPI

PHP

For PHP applications, the SDK is distributed via Composer, the dependency manager for PHP. In your project's root directory, execute:

composer require currentsapi/currents-php

Ensure Composer's autoloader is included in your project:

<?php
require 'vendor/autoload.php';
use CurrentsAPI\CurrentsAPI;
?>

Ruby

The Ruby SDK is available as a Gem. Install it using the gem command:

gem install currentsapi-ruby

Then, require it in your Ruby code:

require 'currentsapi-ruby'

Go

For Go projects, the SDK can be fetched using the go get command:

go get github.com/currentsapi/currents-go

You can then import and use it within your Go source files:

import "github.com/currentsapi/currents-go"

It is generally recommended to manage Go dependencies using modules, which ensure reproducible builds by tracking exact versions of dependencies, a practice detailed in the official Go modules documentation.

Quickstart example

This quickstart demonstrates how to use the Node.js SDK to fetch the latest news articles from the Currents API. Ensure you have installed the Node.js SDK as described in the Installation section and have a valid API key from your Currents dashboard.

Node.js Quickstart

First, create a new JavaScript file (e.g., getNews.js) and add the following code. Replace YOUR_API_KEY with your actual API key.

const CurrentsAPI = require('@currentsapi/currents-api');

const apiKey = 'YOUR_API_KEY'; // Replace with your actual API key
const currents = new CurrentsAPI(apiKey);

async function fetchLatestNews() {
  try {
    const response = await currents.search({
      language: 'en',
      category: 'technology',
      interval: '7d', // Articles published in the last 7 days
      limit: 5 // Get 5 articles
    });

    console.log('Successfully fetched news articles:');
    if (response.news && response.news.length > 0) {
      response.news.forEach((article, index) => {
        console.log(`
Article ${index + 1}:`);
        console.log(`Title: ${article.title}`);
        console.log(`URL: ${article.url}`);
        console.log(`Description: ${article.description ? article.description.substring(0, 150) + '...' : 'No description.'}`);
        console.log(`Published: ${new Date(article.published).toLocaleDateString()}`);
      });
    } else {
      console.log('No news articles found with the specified criteria.');
    }

  } catch (error) {
    console.error('Error fetching news:', error.message);
    if (error.response && error.response.data) {
      console.error('API Error Details:', error.response.data);
    }
  }
}

fetchLatestNews();

To run this example, save the file and execute it from your terminal:

node getNews.js

This script initializes the Currents API client with your API key and then calls the search method to retrieve news articles. The parameters passed to search allow you to filter results by language, category, publication interval, and the number of articles to return. The try...catch block handles potential errors during the API call, providing robust error reporting that includes specific API error details if available. This pattern of asynchronous API calls with error handling is a fundamental aspect of modern JavaScript development, as discussed in various MDN Web Docs on async functions.

Community libraries

While Currents provides a suite of official SDKs to ensure robust and supported integration, the developer community often contributes additional libraries and wrappers. These community-driven projects can extend the reach of the Currents API to other programming languages, provide specialized utilities, or offer alternative architectural patterns (e.g., reactive programming wrappers, command-line interfaces built on the API).

Community libraries are typically found on platforms like GitHub, npm, PyPI, or RubyGems, and their development status, maintenance, and feature parity with the official API can vary. Developers considering a community library should:

  • Check the project's activity: Look for recent commits, active pull request reviews, and issue resolution to gauge ongoing maintenance.
  • Examine documentation and examples: Good documentation is crucial for understanding how to use the library effectively.
  • Review the codebase: Assess code quality, adherence to best practices, and the presence of tests.
  • Understand licensing: Ensure the library's license is compatible with your project's requirements.

As of the last update, specific widely adopted community libraries for Currents are not officially endorsed or listed on the Currents documentation portal. Developers interested in exploring community contributions are advised to search relevant package repositories (e.g., npm for Node.js, PyPI for Python, GitHub for general open-source projects) using keywords like "Currents API" or "Currents SDK" in conjunction with their desired programming language. For example, a search on GitHub for "currentsapi python" might reveal community projects alongside the official SDK, offering a broader range of tools for integration.