Skip to main content

Code Examples

Copy-paste-ready examples for common API operations in Python (requests), Python (http.client), JavaScript, Go, and cURL. Replace YOUR_RAPIDAPI_KEY with the API key from your RapidAPI dashboard.

Base URL and Authentication

All examples use these variables:

VariableValue
BASE_URLhttps://fast-news-with-previews.p.rapidapi.com
RAPIDAPI_KEYYour x-rapidapi-key value from RapidAPI
RAPIDAPI_HOSTfast-news-with-previews.p.rapidapi.com

Search News by Topic

Python (http.client)

Python's built-in http.client module requires no external dependencies.

import http.client
import json

conn = http.client.HTTPSConnection("fast-news-with-previews.p.rapidapi.com")

headers = {
"x-rapidapi-key": "YOUR_RAPIDAPI_KEY",
"x-rapidapi-host": "fast-news-with-previews.p.rapidapi.com",
"Content-Type": "application/json"
}

conn.request("GET", "/news?topic=AI&limit=5&language=en&geo=us&timeframe=anytime", headers=headers)

res = conn.getresponse()
data = res.read()

if res.status == 200:
articles = json.loads(data.decode("utf-8"))
for article in articles["items"]:
print(f"[{article['source']}] {article['title']}")
print(f" {article['url']}")
print(f" {article['preview'][:100]}...")
else:
error = json.loads(data.decode("utf-8"))
print(f"Error {res.status}: {error['detail']}")

conn.close()

Python (requests)

import requests

BASE_URL = "https://fast-news-with-previews.p.rapidapi.com"
RAPIDAPI_KEY = "YOUR_RAPIDAPI_KEY"

response = requests.get(
f"{BASE_URL}/news",
headers={
"x-rapidapi-key": RAPIDAPI_KEY,
"x-rapidapi-host": "fast-news-with-previews.p.rapidapi.com"
},
params={"topic": "artificial intelligence", "language": "en", "limit": 5}
)

if response.status_code == 200:
data = response.json()
for article in data["items"]:
print(f"[{article['source']}] {article['title']}")
print(f" {article['url']}")
print(f" {article['preview'][:100]}...")
else:
print(f"Error {response.status_code}: {response.json()['detail']}")

JavaScript / Node.js

const RAPIDAPI_KEY = "YOUR_RAPIDAPI_KEY";
const BASE_URL = "https://fast-news-with-previews.p.rapidapi.com";

const response = await fetch(
`${BASE_URL}/news?topic=artificial+intelligence&language=en&limit=5`,
{
headers: {
"x-rapidapi-key": RAPIDAPI_KEY,
"x-rapidapi-host": "fast-news-with-previews.p.rapidapi.com"
}
}
);

if (response.ok) {
const data = await response.json();
for (const article of data.items) {
console.log(`[${article.source}] ${article.title}`);
console.log(` ${article.url}`);
console.log(` ${article.preview.slice(0, 100)}...`);
}
} else {
const error = await response.json();
console.error(`Error ${response.status}: ${error.detail}`);
}

Go

package main

import (
"encoding/json"
"fmt"
"log"
"net/http"
"net/url"
)

func main() {
rapidAPIKey := "YOUR_RAPIDAPI_KEY"
rapidAPIHost := "fast-news-with-previews.p.rapidapi.com"
baseURL := "https://" + rapidAPIHost

reqURL, _ := url.Parse(baseURL + "/news")
q := reqURL.Query()
q.Set("topic", "artificial intelligence")
q.Set("language", "en")
q.Set("limit", "5")
reqURL.RawQuery = q.Encode()

req, _ := http.NewRequest("GET", reqURL.String(), nil)
req.Header.Set("x-rapidapi-key", rapidAPIKey)
req.Header.Set("x-rapidapi-host", rapidAPIHost)

resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()

var result struct {
Count int `json:"count"`
Items []struct {
Title string `json:"title"`
Source string `json:"source"`
URL string `json:"url"`
Preview string `json:"preview"`
} `json:"items"`
}

if resp.StatusCode == 200 {
json.NewDecoder(resp.Body).Decode(&result)
for _, a := range result.Items {
fmt.Printf("[%s] %s\n %s\n", a.Source, a.Title, a.URL)
}
} else {
var errResp struct {
Detail string `json:"detail"`
}
json.NewDecoder(resp.Body).Decode(&errResp)
fmt.Printf("Error %d: %s\n", resp.StatusCode, errResp.Detail)
}
}

cURL

curl -X GET "https://fast-news-with-previews.p.rapidapi.com/news?topic=artificial+intelligence&language=en&limit=5" \
-H "x-rapidapi-key: YOUR_RAPIDAPI_KEY" \
-H "x-rapidapi-host: fast-news-with-previews.p.rapidapi.com"

Python (http.client)

import http.client
import json

conn = http.client.HTTPSConnection("fast-news-with-previews.p.rapidapi.com")
headers = {
"x-rapidapi-key": "YOUR_RAPIDAPI_KEY",
"x-rapidapi-host": "fast-news-with-previews.p.rapidapi.com",
"Content-Type": "application/json"
}
conn.request("GET", "/news/trending?language=en&limit=10", headers=headers)
res = conn.getresponse()
data = json.loads(res.read().decode("utf-8"))
for article in data["items"]:
print(article["title"])
conn.close()

Python (requests)

response = requests.get(
f"{BASE_URL}/news/trending",
headers={
"x-rapidapi-key": RAPIDAPI_KEY,
"x-rapidapi-host": "fast-news-with-previews.p.rapidapi.com"
},
params={"language": "en", "limit": 10}
)
data = response.json()
for article in data["items"]:
print(article["title"])

cURL

curl -X GET "https://fast-news-with-previews.p.rapidapi.com/news/trending?language=en&limit=10" \
-H "x-rapidapi-key: YOUR_RAPIDAPI_KEY" \
-H "x-rapidapi-host: fast-news-with-previews.p.rapidapi.com"

Get News by Topic Category

Python (requests)

response = requests.get(
f"{BASE_URL}/news/topic",
headers={
"x-rapidapi-key": RAPIDAPI_KEY,
"x-rapidapi-host": "fast-news-with-previews.p.rapidapi.com"
},
params={"topic1": "technology", "topic2": "business", "language": "en"}
)
data = response.json()
for article in data["items"]:
print(article["title"])

cURL

curl -X GET "https://fast-news-with-previews.p.rapidapi.com/news/topic?topic1=technology&topic2=business&language=en" \
-H "x-rapidapi-key: YOUR_RAPIDAPI_KEY" \
-H "x-rapidapi-host: fast-news-with-previews.p.rapidapi.com"

Get Local News

Python (requests)

response = requests.get(
f"{BASE_URL}/news/local",
headers={
"x-rapidapi-key": RAPIDAPI_KEY,
"x-rapidapi-host": "fast-news-with-previews.p.rapidapi.com"
},
params={"query": "Tokyo", "language": "en"}
)
data = response.json()
for article in data["items"]:
print(article["title"])

cURL

curl -X GET "https://fast-news-with-previews.p.rapidapi.com/news/local?query=Tokyo&language=en" \
-H "x-rapidapi-key: YOUR_RAPIDAPI_KEY" \
-H "x-rapidapi-host: fast-news-with-previews.p.rapidapi.com"

Filter by Timeframe

All news endpoints support a timeframe parameter. Use it to fetch recent articles only:

# Last 24 hours
curl -X GET "https://fast-news-with-previews.p.rapidapi.com/news?topic=bitcoin&language=en&timeframe=last_24h" \
-H "x-rapidapi-key: YOUR_RAPIDAPI_KEY" \
-H "x-rapidapi-host: fast-news-with-previews.p.rapidapi.com"

# Last 7 days
curl -X GET "https://fast-news-with-previews.p.rapidapi.com/news/trending?language=en&timeframe=last_7d" \
-H "x-rapidapi-key: YOUR_RAPIDAPI_KEY" \
-H "x-rapidapi-host: fast-news-with-previews.p.rapidapi.com"