Skip to main content

Code Examples

Copy-paste-ready examples in Python, JavaScript, Go, and cURL. Replace YOUR_RAPIDAPI_KEY with your key from RapidAPI.

Common Variables

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

Search News by Topic

Python (requests)

import requests

response = requests.get(
"https://fast-news-with-previews.p.rapidapi.com/news",
headers={
"x-rapidapi-key": "YOUR_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:
for article in response.json()["items"]:
print(f"[{article['source']}] {article['title']}")
else:
print(f"Error {response.status_code}: {response.json()['detail']}")

Python (http.client)

Python's built-in http.client 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 = json.loads(res.read().decode("utf-8"))

if res.status == 200:
for article in data["items"]:
print(f"[{article['source']}] {article['title']}")
else:
print(f"Error {res.status}: {data['detail']}")
conn.close()

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 { items } = await response.json();
items.forEach(a => console.log(`[${a.source}] ${a.title}`));
} else {
const { detail } = await response.json();
console.error(`Error ${response.status}: ${detail}`);
}

Go

package main

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

func main() {
reqURL, _ := url.Parse("https://fast-news-with-previews.p.rapidapi.com/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", "YOUR_RAPIDAPI_KEY")
req.Header.Set("x-rapidapi-host", "fast-news-with-previews.p.rapidapi.com")

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

var result struct {
Items []struct {
Title string `json:"title"`
Source string `json:"source"`
} `json:"items"`
}

if resp.StatusCode == 200 {
json.NewDecoder(resp.Body).Decode(&result)
for _, a := range result.Items {
fmt.Printf("[%s] %s\n", a.Source, a.Title)
}
} 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 (requests)

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

JavaScript / Node.js

const response = await fetch(
"https://fast-news-with-previews.p.rapidapi.com/news/trending?language=en&limit=10",
{ headers: { "x-rapidapi-key": "YOUR_RAPIDAPI_KEY", "x-rapidapi-host": "fast-news-with-previews.p.rapidapi.com" } }
);
const { items } = await response.json();
items.forEach(a => console.log(a.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"

Topic Categories

Python (requests)

response = requests.get(
"https://fast-news-with-previews.p.rapidapi.com/news/topic",
headers={
"x-rapidapi-key": "YOUR_RAPIDAPI_KEY",
"x-rapidapi-host": "fast-news-with-previews.p.rapidapi.com"
},
params={"topic1": "technology", "topic2": "business", "language": "en"}
)
for article in response.json()["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"

Local News

Python (requests)

response = requests.get(
"https://fast-news-with-previews.p.rapidapi.com/news/local",
headers={
"x-rapidapi-key": "YOUR_RAPIDAPI_KEY",
"x-rapidapi-host": "fast-news-with-previews.p.rapidapi.com"
},
params={"query": "Tokyo", "language": "en"}
)
for article in response.json()["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"

Advanced Search with Filters

Python (requests)

response = requests.get(
"https://fast-news-with-previews.p.rapidapi.com/news/advanced",
headers={
"x-rapidapi-key": "YOUR_RAPIDAPI_KEY",
"x-rapidapi-host": "fast-news-with-previews.p.rapidapi.com"
},
params={
"topic": "technology",
"language": "en",
"source": "bbc.com,theguardian.com",
"thumbnail_only": "true",
"sort": "published_at_desc",
"limit": 10
}
)
for article in response.json()["items"]:
print(f"[{article['source']}] {article['title']}")

JavaScript / Node.js

const response = await fetch(
"https://fast-news-with-previews.p.rapidapi.com/news/advanced?topic=technology&language=en&exclude_source=msn.com,aol.com&sort=title_asc&limit=15",
{ headers: { "x-rapidapi-key": "YOUR_RAPIDAPI_KEY", "x-rapidapi-host": "fast-news-with-previews.p.rapidapi.com" } }
);
const { items } = await response.json();
items.forEach(a => console.log(`[${a.source}] ${a.title}`));

cURL

# Only from specific sources, with thumbnails
curl -X GET "https://fast-news-with-previews.p.rapidapi.com/news/advanced?topic=technology&language=en&source=bbc.com,reuters.com&thumbnail_only=true&sort=published_at_desc" \
-H "x-rapidapi-key: YOUR_RAPIDAPI_KEY" \
-H "x-rapidapi-host: fast-news-with-previews.p.rapidapi.com"

# Exclude sources, with timeframe
curl -X GET "https://fast-news-with-previews.p.rapidapi.com/news/advanced?topic=climate+change&language=en&exclude_source=msn.com&sort=title_asc&timeframe=last_7d" \
-H "x-rapidapi-key: YOUR_RAPIDAPI_KEY" \
-H "x-rapidapi-host: fast-news-with-previews.p.rapidapi.com"

Timeframe Filtering

All news endpoints support timeframe. 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 on trending
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"