SDKs overview

The GNews API offers developers access to current news articles and top headlines programmatically. While GNews does not provide formally designated "official" SDKs in the traditional sense, it supplies comprehensive documentation with code examples in multiple programming languages. These examples serve as a foundation for developers to integrate the API into their applications. The API itself is RESTful, communicating over HTTP, and responses are formatted in JSON (JSON data format specification). This design allows for direct integration using standard HTTP client libraries available in most programming environments, rather than requiring a dedicated SDK.

The primary method of interaction involves sending HTTP GET requests to specific GNews API endpoints, such as the /search endpoint for news articles or the /top-headlines endpoint for trending news (GNews API documentation). Authentication is managed via an API key included as a query parameter in each request. The flexibility of a RESTful API means developers can choose their preferred HTTP client, whether it's Python's requests library, Node.js's axios, or Java's HttpClient, to build their integration logic.

This page will outline the common approaches for integrating with GNews using standard libraries in various programming languages, treating these well-documented examples as de facto "SDKs" for rapid development. It will also cover any notable community-contributed libraries that extend GNews functionality.

Official SDKs by language

GNews provides detailed code examples that effectively function as language-specific integration guides. These examples demonstrate how to construct requests and parse responses using standard HTTP libraries native to each environment. The following table summarizes these official examples and the common libraries used for direct API interaction.

Language Common Package/Method Installation/Setup Maturity
Python requests library pip install requests Stable (via requests library)
Node.js axios or native fetch API npm install axios or built-in Stable (via axios or fetch API)
PHP cURL extension or HTTP client libraries (e.g., Guzzle) Typically built-in or composer require guzzlehttp/guzzle Stable (via cURL or Guzzle)
Ruby Net::HTTP (standard library) or HTTParty gem Built-in or gem install httparty Stable (via Net::HTTP or HTTParty)
Go net/http package (standard library) Built-in Stable (via net/http)
Java java.net.http.HttpClient (Java 11+) or Apache HttpClient Built-in (Java 11+) or Maven/Gradle dependency for Apache HttpClient Stable (via HttpClient)

Installation

Installation typically involves adding an HTTP client library to your project, as GNews itself does not offer installable SDK packages. Below are common installation commands for the recommended HTTP clients for each language:

Python

The requests library simplifies HTTP requests in Python (Python Requests documentation). Install it using pip:

pip install requests

Node.js

axios is a popular promise-based HTTP client for Node.js and browsers. Alternatively, Node.js versions 18 and higher include a native fetch API (Mozilla Fetch API reference).

npm install axios
# Or, for native fetch (Node.js 18+):
# No installation needed

PHP

PHP's cURL extension is often enabled by default. For a more modern and robust HTTP client, Guzzle is widely used.

# Check if cURL is enabled: php -m | grep curl
# To install Guzzle via Composer:
composer require guzzlehttp/guzzle

Ruby

Ruby's standard library includes Net::HTTP. For a more user-friendly interface, HTTParty is a popular choice.

# No installation needed for Net::HTTP
# To install HTTParty:
gem install httparty

Go

Go's standard library provides the net/http package for making HTTP requests.

# No installation needed; it's part of the standard library

Java

For Java 11 and newer, the built-in java.net.http.HttpClient is recommended. For older Java versions or more advanced features, Apache HttpClient is a common dependency.

Maven (for Apache HttpClient):

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.13</version> <!-- Use the latest stable version -->
</dependency>

Gradle (for Apache HttpClient):

implementation 'org.apache.httpcomponents:httpclient:4.5.13' // Use the latest stable version

Quickstart example

This example demonstrates how to fetch top headlines using the GNews API in Python. You will need a GNews API key (GNews API Key acquisition) to make requests.

Python example (using requests)

import requests
import json

API_KEY = 'YOUR_API_KEY' # Replace with your actual API key
QUERY = 'technology'
LANG = 'en'
COUNTRY = 'us'
CATEGORY = 'breaking-news'
MAX_ARTICLES = 5

BASE_URL = 'https://gnews.io/api/v4/top-headlines'

params = {
    'q': QUERY,
    'lang': LANG,
    'country': COUNTRY,
    'category': CATEGORY,
    'max': MAX_ARTICLES,
    'apikey': API_KEY
}

try:
    response = requests.get(BASE_URL, params=params)
    response.raise_for_status() # Raise an exception for HTTP errors
    data = response.json()

    print(f"Found {len(data.get('articles', []))} top headlines for '{QUERY}':")
    for article in data.get('articles', [])[:MAX_ARTICLES]:
        print(f"- {article['title']} ({article['url']})")

except requests.exceptions.HTTPError as http_err:
    print(f"HTTP error occurred: {http_err}")
    if response.status_code == 403:
        print("API Key might be invalid or access denied. Check your GNews API key.")
except requests.exceptions.RequestException as req_err:
    print(f"Request error occurred: {req_err}")
except json.JSONDecodeError:
    print("Failed to decode JSON response.")
except Exception as err:
    print(f"An unexpected error occurred: {err}")

This Python script:

  1. Imports the requests and json libraries.
  2. Sets your GNews API key and other query parameters.
  3. Constructs the API request URL for top headlines.
  4. Sends an HTTP GET request to the GNews API.
  5. Checks for HTTP errors and parses the JSON response.
  6. Prints the titles and URLs of the retrieved articles.
  7. Includes error handling for common issues like network problems or invalid API keys.

For additional language examples, refer to the GNews official documentation.

Community libraries

As the GNews API is a straightforward RESTful service, the direct use of standard HTTP client libraries is often sufficient, leading to fewer third-party community-driven SDKs compared to more complex APIs. However, developers may create wrappers or utility libraries to further abstract the API calls or integrate GNews with specific frameworks. These community contributions are typically found on platforms like GitHub or package managers (e.g., PyPI for Python, npm for Node.js).

When considering a community library, it is advisable to evaluate its:

  • Maintenance status: Check the last commit date and activity.
  • Documentation: Ensure clear instructions and examples are provided.
  • Community support: Look for active issue trackers or forums.
  • Licensing: Understand the terms under which the library is distributed.

As of late 2026, there are no widely adopted or officially recognized community SDKs for GNews that significantly differ from the direct API integration methods outlined in their primary language examples. Developers are encouraged to consult their respective language's package repositories and GitHub for any emerging community projects that might simplify GNews interactions for specific use cases.