trymonolith commited on
Commit
7130d2e
Β·
verified Β·
1 Parent(s): ffb01eb

Add comprehensive API documentation with all endpoints and examples

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