stophy 0.2.1 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +59 -69
- package/dist/index.cjs +562 -282
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +22754 -664
- package/dist/index.d.ts +22754 -664
- package/dist/index.js +562 -282
- package/dist/index.js.map +1 -1
- package/package.json +13 -10
package/README.md
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
Official TypeScript SDK for [Stophy](https://stophy.dev) **YouTube context API for AI agents**. Search videos, fetch transcripts, read comments and live chat, inspect channels and playlists, and get autocomplete suggestions, all returned as structured JSON.
|
|
1
|
+
# Stophy for TypeScript
|
|
4
2
|
|
|
3
|
+
Get public web data in your TypeScript or JavaScript code: search results, videos, social posts, places, products, jobs, homes, and more. Every method and result is typed.
|
|
5
4
|
|
|
6
5
|
## Install
|
|
7
6
|
|
|
@@ -9,98 +8,89 @@ Official TypeScript SDK for [Stophy](https://stophy.dev) **YouTube context API
|
|
|
9
8
|
npm install stophy
|
|
10
9
|
```
|
|
11
10
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
## Quick start
|
|
11
|
+
## Try it without a key
|
|
15
12
|
|
|
16
13
|
```ts
|
|
17
14
|
import { Stophy } from "stophy";
|
|
18
15
|
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
"https://www.youtube.com/watch?v=D7liwdjvhWc",
|
|
22
|
-
);
|
|
23
|
-
console.log(result.data.text);
|
|
16
|
+
const result = await new Stophy().web.search({ query: "bun runtime" });
|
|
17
|
+
console.log(result.data.results);
|
|
24
18
|
```
|
|
25
19
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
| Method | Description |
|
|
29
|
-
| --- | --- |
|
|
30
|
-
| `stophy.videoDetails(url)` | Video metadata and related videos |
|
|
31
|
-
| `stophy.transcript(url)` | Timestamped captions and full text |
|
|
32
|
-
| `stophy.comments(url, options?)` | Top-level comments |
|
|
33
|
-
| `stophy.replies(token)` | Replies for a comment thread |
|
|
34
|
-
| `stophy.liveChat(url, options?)` | Live stream chat messages |
|
|
35
|
-
| `stophy.search(query, options?)` | Search with filters |
|
|
36
|
-
| `stophy.channel(url, options?)` | Channel metadata and content |
|
|
37
|
-
| `stophy.playlist(url, options?)` | Playlist items, paginated |
|
|
38
|
-
| `stophy.suggest(query, options?)` | Search autocomplete suggestions |
|
|
39
|
-
| `stophy.credits()` | Current credit balance |
|
|
40
|
-
| `stophy.logs(query?)` | Recent request logs |
|
|
41
|
-
| `stophy.usage(query?)` | Daily credit/request counts |
|
|
42
|
-
|
|
43
|
-
### Examples
|
|
20
|
+
Web search, YouTube search, and YouTube transcripts work without a key, with a small free allowance. Every other method throws a `StophyError` with the code `unauthorized`.
|
|
44
21
|
|
|
45
|
-
|
|
46
|
-
// Search
|
|
47
|
-
const results = await stophy.search("typescript tutorial", {
|
|
48
|
-
sortBy: "popularity",
|
|
49
|
-
duration: "long",
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
// Comments (top-level), then replies to a comment.
|
|
53
|
-
// `video()` is overloaded on `type`, so `data` is typed — no casts needed.
|
|
54
|
-
const comments = await stophy.comments(videoUrl, { sortBy: "top" });
|
|
55
|
-
const firstReplyToken = comments.data.items[0]?.repliesToken;
|
|
56
|
-
if (firstReplyToken) {
|
|
57
|
-
const replies = await stophy.replies(firstReplyToken);
|
|
58
|
-
}
|
|
22
|
+
## Use an API key
|
|
59
23
|
|
|
60
|
-
|
|
61
|
-
const channel = await stophy.channel("https://www.youtube.com/@mkbhd", {
|
|
62
|
-
tab: "video",
|
|
63
|
-
});
|
|
24
|
+
Get a key from the [dashboard](https://stophy.dev/dashboard). Keys start with `st_`. Set it as `STOPHY_API_KEY`, or pass it in:
|
|
64
25
|
|
|
65
|
-
|
|
66
|
-
const
|
|
67
|
-
console.log(s?.suggestions);
|
|
26
|
+
```ts
|
|
27
|
+
const stophy = new Stophy({ apiKey: "st_..." });
|
|
68
28
|
|
|
69
|
-
|
|
70
|
-
console.log(
|
|
29
|
+
const videos = await stophy.youtube.search({ query: "bun runtime", limit: 5 });
|
|
30
|
+
console.log(videos.data.results);
|
|
71
31
|
```
|
|
72
32
|
|
|
73
|
-
|
|
33
|
+
`new Stophy("st_...")` works too.
|
|
74
34
|
|
|
75
|
-
|
|
35
|
+
Methods follow the source and the command: `stophy.maps.search(...)`, `stophy.reddit.subreddit(...)`, `stophy.youtube.comments.replies(...)`. Each result has `data`, `creditsUsed`, and `requestId`.
|
|
36
|
+
|
|
37
|
+
## Get markdown for a model
|
|
76
38
|
|
|
77
39
|
```ts
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
token = page.data?.continuationToken ?? undefined;
|
|
83
|
-
} while (token);
|
|
40
|
+
const markdown = await stophy.youtube.search(
|
|
41
|
+
{ query: "bun runtime", limit: 5 },
|
|
42
|
+
{ format: "markdown" },
|
|
43
|
+
);
|
|
84
44
|
```
|
|
85
45
|
|
|
86
|
-
|
|
46
|
+
With `format: "markdown"`, the method returns a string.
|
|
47
|
+
|
|
48
|
+
## Get the next page
|
|
49
|
+
|
|
50
|
+
When there are more results, `data.cursor` is set. Pass it back to get the next page:
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
const first = await stophy.reddit.search({ query: "bun" });
|
|
54
|
+
const next = await stophy.reddit.search({ query: "bun", cursor: first.data.cursor });
|
|
55
|
+
```
|
|
87
56
|
|
|
88
|
-
|
|
57
|
+
## Handle errors
|
|
89
58
|
|
|
90
59
|
```ts
|
|
91
|
-
import {
|
|
60
|
+
import { StophyError } from "stophy";
|
|
92
61
|
|
|
93
62
|
try {
|
|
94
|
-
await stophy.
|
|
95
|
-
} catch (
|
|
96
|
-
if (
|
|
97
|
-
console.error
|
|
98
|
-
// err.code: "UNAUTHORIZED" | "INSUFFICIENT_CREDITS" | "BAD_REQUEST" |
|
|
99
|
-
// "INVALID_INPUT" | "NOT_FOUND" | "CONCURRENCY_LIMITED" | "INTERNAL_ERROR"
|
|
63
|
+
await stophy.reddit.search({ query: "bun" });
|
|
64
|
+
} catch (error) {
|
|
65
|
+
if (error instanceof StophyError) {
|
|
66
|
+
console.log(error.code, error.retryable, error.retryAfterSeconds);
|
|
100
67
|
}
|
|
101
68
|
}
|
|
102
69
|
```
|
|
103
70
|
|
|
71
|
+
`StophyError` has `code`, `message`, `retryable`, `retryAfterSeconds`, `status`, and `requestId`.
|
|
72
|
+
|
|
73
|
+
The SDK retries network errors and the HTTP statuses 429, 500, 502, 503, and 504, and it waits as long as the API asks. If the API asks for a wait longer than 60 seconds, the SDK throws right away with `retryAfterSeconds` set.
|
|
74
|
+
|
|
75
|
+
## Options
|
|
76
|
+
|
|
77
|
+
| Option | Default | What it does |
|
|
78
|
+
| --- | --- | --- |
|
|
79
|
+
| `apiKey` | `STOPHY_API_KEY` | Your API key |
|
|
80
|
+
| `baseUrl` | `STOPHY_BASE_URL`, or the Stophy API | The server to call |
|
|
81
|
+
| `maxRetries` | `2` | Retries per request. `0` turns retries off. |
|
|
82
|
+
| `timeoutMs` | `30000` | Time limit for each attempt. A timed-out attempt is not retried. |
|
|
83
|
+
|
|
84
|
+
Each method also takes `signal` to cancel the request.
|
|
85
|
+
|
|
86
|
+
## Check your account
|
|
87
|
+
|
|
88
|
+
```ts
|
|
89
|
+
const usage = await stophy.usage();
|
|
90
|
+
const logs = await stophy.logs({ days: 7, page: 0 });
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
`usage` returns your balance and your all-time usage. `logs` returns your recent requests. Both need an API key.
|
|
104
94
|
|
|
105
95
|
## License
|
|
106
96
|
|