Files
sdk/sdks/javascript/README.md
T

105 lines
3.2 KiB
Markdown
Raw Normal View History

2026-05-29 12:46:42 -05:00
# @socialhose/api
TypeScript SDK for the Socialhose Public API.
2026-05-29 13:35:09 -05:00
Use it from backend TypeScript/JavaScript to fetch campaigns, analytics, mentions, mailing lists, and SDK-composed entity analytics with authentication, retries, timeouts, and caching handled consistently.
2026-05-29 12:46:42 -05:00
## Install
```bash
npm install @socialhose/api
```
2026-05-29 13:35:09 -05:00
Node 18+ is required because the SDK uses built-in `fetch`, `Response`, and `AbortSignal.timeout`. You can pass a custom `fetch` implementation if needed.
2026-05-29 12:46:42 -05:00
## Quickstart
```ts
import { SocialhoseClient } from '@socialhose/api';
const socialhose = new SocialhoseClient({
apiKey: process.env.SOCIALHOSE_API_KEY!,
});
const mentions = await socialhose.getMentions({
content_search: 'hospital',
platforms: 'twitter',
ordering: '-published_at',
});
console.log(mentions.count, mentions.results[0]?.content);
```
2026-05-29 13:35:09 -05:00
## Documentation
- [Usage guide](./docs/GUIDE.md) — what the SDK is, setup, common workflows, operational guidance.
- [API reference](./docs/API.md) — every public class, function, method, option, and exported type.
- [Entity analytics](./docs/ENTITY_ANALYTICS.md) — how term-level analytics are built, accuracy safeguards, rate-limit guidance.
- [Caching and retries](./docs/CACHING_AND_RETRIES.md) — cache contract, retry policy, failure semantics.
- [Examples](./examples) — runnable TypeScript examples.
2026-05-29 12:46:42 -05:00
## Configuration
```ts
const socialhose = new SocialhoseClient({
apiKey: process.env.SOCIALHOSE_API_KEY!,
baseUrl: 'https://socialhose.net/api/public/v1',
timeoutMs: 8_000,
retries: 3,
cacheTtlMs: 60_000,
});
```
The SDK sends `Authorization: Api-Key <key>` and a browser-like `User-Agent` by default. The user-agent is intentional: the current Socialhose API edge rejects some non-browser requests.
2026-05-29 13:35:09 -05:00
## Main capabilities
2026-05-29 13:35:09 -05:00
- Campaign discovery: `getCampaigns()`, `getCampaign(id)`, `getCampaignIdByMatch(match)`.
- Analytics: `getOverview()`, `getTimeline()`, `getSentiment()`, `getShareOfVoice()`, `getPlatforms()`, `getTopKeywords()`, `getTrending()`, `getTopMentions()`.
- Mentions: `getMentions()` with campaign, date, platform, sentiment, text search, pagination, and ordering filters.
- Mailing lists: `getMailingLists()`, `inviteMailingListMember()`.
- Entity analytics: `getEntityBrief()`, `getEntityStats()`, `getEntityBriefs()`.
- Low-level access: `get(path, params)`, `post(path, body)`.
- Caching: built-in `MemoryCache`, disabling via `NoopCache`, or any custom `Cache` implementation.
2026-05-29 13:35:09 -05:00
## Entity analytics warning
2026-05-29 13:35:09 -05:00
Entity analytics fan out multiple `/mentions/` requests per term. Use `cacheTtlMs`, `revalidateSeconds`, or a persistent shared cache to stay under the approximate 60 req/min API-key rate limit.
2026-05-29 12:46:42 -05:00
```ts
2026-05-29 13:35:09 -05:00
const stats = await socialhose.getEntityStats('RSF', 'campaign-id', {
revalidateSeconds: 900,
2026-05-29 12:46:42 -05:00
});
```
## Errors
2026-05-29 13:35:09 -05:00
Failed requests throw `SocialhoseError` with `status`, `path`, `body`, and `cause` when available.
2026-05-29 12:46:42 -05:00
```ts
import { SocialhoseError } from '@socialhose/api';
try {
await socialhose.getCampaign('missing');
} catch (error) {
if (error instanceof SocialhoseError) {
console.error(error.status, error.path, error.body);
}
}
```
2026-05-29 13:35:09 -05:00
## Development
2026-05-29 12:46:42 -05:00
```bash
2026-05-29 13:35:09 -05:00
pnpm install
2026-05-29 12:46:42 -05:00
pnpm test
pnpm typecheck
pnpm build
```
2026-05-29 13:35:09 -05:00
Publishing:
```bash
npm publish --access public --provenance
```