Spaces:
Sleeping
Sleeping
File size: 10,230 Bytes
9db8530 7130d2e 9db8530 7130d2e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 | ---
title: Pytrends API
emoji: π
colorFrom: purple
colorTo: blue
sdk: docker
pinned: false
short_description: Self-hosted pytrends API for Google Trends data via HTTP
---
# PyTrends HTTP API
A self-hosted REST API for Google Trends data powered by [pytrends](https://github.com/GeneralMills/pytrends). Get trend data, regional interest, trending searches, and related queries programmatically via simple HTTP endpoints.
## π Features
- β
**5 REST Endpoints** - Comprehensive Google Trends data access
- β
**Multiple Keywords** - Search for trends across multiple keywords simultaneously
- β
**Geographic Filtering** - Get data for specific countries or regions
- β
**Time Range Selection** - Choose from various timeframes
- β
**JSON Responses** - Easy integration with automation platforms
- β
**Self-Hosted** - Run on your own infrastructure
- β
**Make.com & n8n Ready** - Built for automation workflows
- β
**Docker Deployed** - Containerized on Hugging Face Spaces
## π API Endpoints
### 1. Health Check
**Endpoint:** `GET /health`
Check if the API is running and healthy.
**Response:**
```json
{
"status": "healthy"
}
```
---
### 2. Root Endpoint
**Endpoint:** `GET /`
Get information about available endpoints.
**Response:**
```json
{
"message": "PyTrends API",
"endpoints": {
"/interest_over_time": "Get interest over time for keywords",
"/interest_by_region": "Get interest by region for keywords",
"/trending_searches": "Get trending searches for a country",
"/related_queries": "Get related queries for keywords"
}
}
```
---
### 3. Interest Over Time
**Endpoint:** `GET /interest_over_time`
Get search interest trends over time for specified keywords.
**Parameters:**
| Parameter | Type | Required | Description |
|-----------|------|----------|-------------|
| `kw` | string[] | Yes | Keywords to search (repeat parameter for multiple keywords) |
| `timeframe` | string | No | Time range for data (Default: "today 5-y"). Examples: "today 1-m", "today 3-m", "today 1-y", "2020-01-01 2020-12-31" |
| `geo` | string | No | Geographic location code (Empty = worldwide). Examples: "US", "IN", "GB", "DE" |
| `gprop` | string | No | Google property filter (Empty = all). Options: "images", "news", "youtube", "shopping" |
| `cat` | integer | No | Category ID (Default: 0 = all categories) |
**Example Request:**
```
GET /interest_over_time?kw=bitcoin&kw=ethereum&timeframe=today%205-y&geo=US
```
**Response:**
```json
{
"data": [
{
"date": "2020-01-04 00:00:00",
"bitcoin": 41,
"ethereum": 33
},
{
"date": "2020-01-11 00:00:00",
"bitcoin": 42,
"ethereum": 33
}
],
"keywords": ["bitcoin", "ethereum"],
"timeframe": "today 5-y",
"geo": "US"
}
```
---
### 4. Interest By Region
**Endpoint:** `GET /interest_by_region`
Get search interest distribution by geographic region.
**Parameters:**
| Parameter | Type | Required | Description |
|-----------|------|----------|-------------|
| `kw` | string[] | Yes | Keywords to search |
| `timeframe` | string | No | Time range (Default: "today 5-y") |
| `geo` | string | No | Geographic location (Empty = worldwide) |
| `gprop` | string | No | Google property filter |
| `resolution` | string | No | Resolution level (Default: "country"). Options: "country", "region", "metro", "city" |
**Example Request:**
```
GET /interest_by_region?kw=python&resolution=country&timeframe=today%201-y
```
**Response:**
```json
{
"data": [
{
"geoName": "India",
"python": 100
},
{
"geoName": "Pakistan",
"python": 54
},
{
"geoName": "Philippines",
"python": 47
}
],
"keywords": ["python"],
"resolution": "country"
}
```
---
### 5. Trending Searches
**Endpoint:** `GET /trending_searches`
Get currently trending searches in a specific country.
**Parameters:**
| Parameter | Type | Required | Description |
|-----------|------|----------|-------------|
| `country` | string | No | Country code (Default: "united_states"). Examples: "united_states", "india", "united_kingdom", "canada" |
**Supported Countries:**
- united_states
- india
- united_kingdom
- canada
- australia
- japan
- germany
- france
- brazil
- mexico
- south_korea
- and many more...
**Example Request:**
```
GET /trending_searches?country=india
```
**Response:**
```json
{
"trending": [
"nykaa fashion",
"maharashtra election results",
"atal pension yojana",
"deepika padukone",
"india news"
],
"country": "india"
}
```
---
### 6. Related Queries
**Endpoint:** `GET /related_queries`
Get related search queries and their frequency for specified keywords.
**Parameters:**
| Parameter | Type | Required | Description |
|-----------|------|----------|-------------|
| `kw` | string[] | Yes | Keywords to search |
| `timeframe` | string | No | Time range (Default: "today 5-y") |
| `geo` | string | No | Geographic location (Empty = worldwide) |
**Example Request:**
```
GET /related_queries?kw=machine%20learning&timeframe=today%203-m
```
**Response:**
```json
{
"related_queries": {
"machine learning": {
"top": [
{
"query": "machine learning python",
"value": 100
},
{
"query": "machine learning course",
"value": 75
}
],
"rising": [
{
"query": "machine learning for beginners",
"value": 150
},
{
"query": "machine learning with tensorflow",
"value": 130
}
]
}
},
"keywords": ["machine learning"]
}
```
---
## π Base URL
```
https://trymonolith-pytrends-api.hf.space
```
## π Usage Examples
### cURL
```bash
# Get interest over time
curl "https://trymonolith-pytrends-api.hf.space/interest_over_time?kw=python&kw=javascript&timeframe=today%201-y&geo=US"
# Get trending searches in India
curl "https://trymonolith-pytrends-api.hf.space/trending_searches?country=india"
# Get interest by region
curl "https://trymonolith-pytrends-api.hf.space/interest_by_region?kw=ai&resolution=country"
```
### Python
```python
import requests
base_url = "https://trymonolith-pytrends-api.hf.space"
# Get interest over time
response = requests.get(
f"{base_url}/interest_over_time",
params={
"kw": ["bitcoin", "ethereum"],
"timeframe": "today 5-y",
"geo": "US"
}
)
data = response.json()
print(data)
```
### JavaScript
```javascript
const baseUrl = "https://trymonolith-pytrends-api.hf.space";
// Get trending searches
fetch(`${baseUrl}/trending_searches?country=united_states`)
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error("Error:", error));
```
---
## π€ Integration Guides
### Make.com
1. Add **HTTP > Make a request** module to your workflow
2. Set **Method:** GET
3. Set **URL:** `https://trymonolith-pytrends-api.hf.space/interest_over_time`
4. Add **Query parameters:**
- `kw`: bitcoin (repeat for multiple keywords)
- `timeframe`: today 5-y
- `geo`: US
5. Enable **Parse response** to auto-map JSON fields
6. Use output in downstream modules (Google Sheets, email, etc.)
### n8n
1. Add **HTTP Request** node
2. Set **Method:** GET
3. Set **URL:** `https://trymonolith-pytrends-api.hf.space/interest_by_region`
4. Go to **Params** tab and add:
- Key: `kw`, Value: `python`
- Key: `resolution`, Value: `country`
5. Click **Execute** to test
6. Connect to other nodes (e.g., Save to database, Send email)
### Zapier
1. Use **Webhooks by Zapier** β **Catch Raw Hook**
2. Or use **HTTP** action with GET method
3. URL: `https://trymonolith-pytrends-api.hf.space/trending_searches?country=india`
4. Parse JSON response and use in your Zap
---
## β±οΈ Uptime & Spindown
**Hardware:** CPU Basic (Free tier)
- **Active Duration:** Runs indefinitely as long as there are requests
- **Inactivity Timeout:** 48 hours
- **Automatic Restart:** Yes - Space restarts automatically when accessed after being paused
- **Startup Time:** ~30-60 seconds after restart
**Recommendation:** For production use with guaranteed uptime, upgrade to paid CPU hardware ($0.03/hour β $22/month).
---
## π HTTP Status Codes
| Code | Meaning | Description |
|------|---------|-------------|
| 200 | OK | Request successful, data returned |
| 400 | Bad Request | Invalid parameters or missing required fields |
| 500 | Internal Server Error | Server error processing your request |
---
## π§ Response Format
All endpoints return JSON responses. Successful responses include:
- `data` - Array of results or object containing data
- `keywords` - The keywords searched
- Additional metadata fields depending on endpoint
Error responses follow this format:
```json
{
"error": "Descriptive error message"
}
```
---
## π Timeframe Options
- `today 1-m` - Last 30 days
- `today 3-m` - Last 90 days
- `today 1-y` - Last year
- `today 5-y` - Last 5 years (default)
- `2020-01-01 2020-12-31` - Custom date range (YYYY-MM-DD format)
---
## π‘οΈ Rate Limiting
Currently, there are no rate limits implemented. However, pytrends may have requests throttled by Google at very high volumes.
---
## π Troubleshooting
### API Returns Empty Data
- Check that your keywords are spelled correctly
- Try with a longer timeframe
- Ensure the geographic location code is valid
### "Space Paused" Error
- The Space has been inactive for 48+ hours
- Make a simple request to `/health` to restart it
- Wait 30-60 seconds for startup
### Related Queries Endpoint Returns Partial Data
- Some keywords may not have related query data
- The endpoint returns data for keywords that have it
---
## π License
This project uses the [pytrends](https://github.com/GeneralMills/pytrends) library (MIT License).
---
## π Links
- **GitHub Repo:** Available in HF Spaces
- **PyTrends Library:** https://github.com/GeneralMills/pytrends
- **Hugging Face Spaces:** https://huggingface.co/spaces
---
## π Support
For issues or suggestions:
1. Check this documentation first
2. Review the API response errors
3. Verify your parameters are correct
4. Try with simpler keywords or timeframes |