1
1
# Beehiiv TypeScript Library
2
2
3
- [ ![ fern shield] ( https://img.shields.io/badge/%F0%9F%8C%BF-SDK%20generated%20by% 20Fern-brightgreen )] ( https://buildwithfern.com/ )
4
- [ ![ npm shield] ( https://img.shields.io/npm/v/beehiiv )] ( https://www.npmjs.com/package/beehiiv )
3
+ [ ![ fern shield] ( https://img.shields.io/badge/%F0%9F%8C%BF-Built%20with% 20Fern-brightgreen )] ( https://buildwithfern.com?utm_source=github&utm_medium=github&utm_campaign=readme&utm_source=https%3A%2F%2Fgithub.com%2Fbeehiiv%2Ftypescript-sdk )
4
+ [ ![ npm shield] ( https://img.shields.io/npm/v/@ beehiiv/sdk )] ( https://www.npmjs.com/package/@ beehiiv/sdk )
5
5
6
- The Beehiiv TypeScript library provides convenient access to the Beehiiv Public API from JavaScript/ TypeScript.
6
+ The Beehiiv TypeScript library provides convenient access to the Beehiiv API from TypeScript.
7
7
8
8
## Documentation
9
9
10
10
API reference documentation is available [ here] ( https://developers.beehiiv.com/docs/v2/o3elujhmkik1d-beehiiv-api ) .
11
11
12
- ## Reference
13
-
14
- A full reference of the SDK is available [ here] ( ./reference.md ) .
15
-
16
12
## Installation
17
13
18
- ``` bash
19
- npm install --save beehiiv
20
- # or
21
- yarn add beehiiv
14
+ ``` sh
15
+ npm i -s @beehiiv/sdk
22
16
```
23
17
24
- In Deno (1.25+) you can import by doing:
18
+ ## Reference
25
19
26
- ``` ts
27
- import { Beehiv } from " npm:beehiv" ;
28
- ```
20
+ A full reference for this library is available [ here] ( ./reference.md ) .
29
21
30
22
## Usage
31
23
32
- ``` typescript
33
- import { BeehiivClient , Beehiiv } from ' beehiiv' ;
24
+ Instantiate and use the client with the following:
34
25
35
- const beehiiv = new BeehiivClient ({
36
- token: " token..." , // Defaults to process.env.BEEHIIV_API_KEY
37
- });
26
+ ``` typescript
27
+ import { BeehiivClient } from " @beehiiv/sdk" ;
38
28
39
- const response = await beehiiv .subscriptions .create (
40
- " pub_11c6387f-1d31-40c7-85ee-4a2da0c63001" ,
29
+ const client = new BeehiivClient ({ token: " YOUR_TOKEN" });
30
+ await client .automationJourneys .create (
31
+ " pub_00000000-0000-0000-0000-000000000000" ,
32
+ " aut_00000000-0000-0000-0000-000000000000" ,
41
33
{
42
- email: " ...@email.com" ,
43
- },
34
+ email: " test@example.com" ,
35
+ doubleOptOverride: " on" ,
36
+ }
44
37
);
45
38
```
46
39
47
- ## Request and Response Types
40
+ ## Request And Response Types
48
41
49
- The SDK exports all request and response types as TypeScript interfaces. Simply
50
- import them under the ` Beehiiv ` namespace:
42
+ The SDK exports all request and response types as TypeScript interfaces. Simply import them with the
43
+ following namespace:
51
44
52
- ``` ts
53
- import { Beehiiv } from " beehiiv" ;
45
+ ``` typescript
46
+ import { Beehiiv } from " @ beehiiv/sdk " ;
54
47
55
- const publication: Beehiiv .Publication = {
56
- id: " pub_..." ,
57
- name: " Some publication" ,
58
- referralProgramEnabled: true ,
59
- created: 1713226240
48
+ const request: Beehiiv .AutomationJourneysCreateRequest = {
49
+ ...
60
50
};
61
51
```
62
52
63
53
## Exception Handling
64
54
65
- When the API returns a non-success status code (4xx or 5xx response),
66
- a subclass of [ BeehiivError ] ( ./src/errors/BeehiivError.ts ) will be thrown:
55
+ When the API returns a non-success status code (4xx or 5xx response), a subclass of the following error
56
+ will be thrown.
67
57
68
- ``` ts
69
- import { BeehiivError } from ' beehiiv' ;
58
+ ``` typescript
59
+ import { BeehiivError } from " @ beehiiv/sdk " ;
70
60
71
61
try {
72
- await beehiiv .subscriptions .create (
73
- " pub_11c6387f-1d31-40c7-85ee-4a2da0c63001" ,
74
- {
75
- email: " ...@email.com" ,
76
- },
77
- );
62
+ await client .automationJourneys .create (... );
78
63
} catch (err ) {
79
- if (err instanceof BeehiivError ) {
80
- console .log (err .statusCode );
81
- console .log (err .message );
82
- console .log (err .body );
83
- }
64
+ if (err instanceof BeehiivError ) {
65
+ console .log (err .statusCode );
66
+ console .log (err .message );
67
+ console .log (err .body );
68
+ }
84
69
}
85
70
```
86
71
72
+ ## Advanced
73
+
74
+ ### Retries
75
+
76
+ The SDK is instrumented with automatic retries with exponential backoff. A request will be retried as long
77
+ as the request is deemed retriable and the number of retry attempts has not grown larger than the configured
78
+ retry limit (default: 2).
79
+
80
+ A request is deemed retriable when any of the following HTTP status codes is returned:
81
+
82
+ - [ 408] ( https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/408 ) (Timeout)
83
+ - [ 429] ( https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/429 ) (Too Many Requests)
84
+ - [ 5XX] ( https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500 ) (Internal Server Errors)
85
+
86
+ Use the ` maxRetries ` request option to configure this behavior.
87
+
88
+ ``` typescript
89
+ const response = await client .automationJourneys .create (... , {
90
+ maxRetries: 0 // override maxRetries at the request level
91
+ });
92
+ ```
93
+
94
+ ### Timeouts
95
+
96
+ The SDK defaults to a 60 second timeout. Use the ` timeoutInSeconds ` option to configure this behavior.
97
+
98
+ ``` typescript
99
+ const response = await client .automationJourneys .create (... , {
100
+ timeoutInSeconds: 30 // override timeout to 30s
101
+ });
102
+ ```
103
+
104
+ ### Aborting Requests
105
+
106
+ The SDK allows users to abort requests at any point by passing in an abort signal.
107
+
108
+ ``` typescript
109
+ const controller = new AbortController ();
110
+ const response = await client .automationJourneys .create (... , {
111
+ abortSignal: controller .signal
112
+ });
113
+ controller .abort (); // aborts the request
114
+ ```
115
+
116
+ ### Runtime Compatibility
117
+
118
+ The SDK defaults to ` node-fetch ` but will use the global fetch client if present. The SDK works in the following
119
+ runtimes:
120
+
121
+ - Node.js 18+
122
+ - Vercel
123
+ - Cloudflare Workers
124
+ - Deno v1.25+
125
+ - Bun 1.0+
126
+ - React Native
127
+
128
+ ### Customizing Fetch Client
129
+
130
+ The SDK provides a way for your to customize the underlying HTTP client / Fetch function. If you're running in an
131
+ unsupported environment, this provides a way for you to break glass and ensure the SDK works.
132
+
133
+ ``` typescript
134
+ import { BeehiivClient } from " @beehiiv/sdk" ;
135
+
136
+ const client = new BeehiivClient ({
137
+ ...
138
+ fetcher : // provide your implementation here
139
+ });
140
+ ```
141
+
87
142
## Retries
88
143
89
144
The TypeScript SDK is instrumented with automatic retries with exponential backoff. A request will be
@@ -92,12 +147,12 @@ than the configured retry limit (default: 2).
92
147
93
148
A request is deemed retriable when any of the following HTTP status codes is returned:
94
149
95
- - [ 408] ( https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/408 ) (Timeout)
96
- - [ 409] ( https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/409 ) (Conflict)
97
- - [ 429] ( https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/429 ) (Too Many Requests)
98
- - [ 5XX] ( https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500 ) (Internal Server Errors)
99
-
100
- Use the ` maxRetries ` request option to configure this behavior.
150
+ - [ 408] ( https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/408 ) (Timeout)
151
+ - [ 409] ( https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/409 ) (Conflict)
152
+ - [ 429] ( https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/429 ) (Too Many Requests)
153
+ - [ 5XX] ( https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500 ) (Internal Server Errors)
154
+
155
+ Use the ` maxRetries ` request option to configure this behavior.
101
156
102
157
``` ts
103
158
const response = await beehiiv .subscriptions .create (... , {
@@ -107,8 +162,8 @@ const response = await beehiiv.subscriptions.create(..., {
107
162
108
163
## Timeouts
109
164
110
- The SDK defaults to a 60 second timout. Use the ` timeoutInSeconds ` option to
111
- configure this behavior.
165
+ The SDK defaults to a 60 second timout. Use the ` timeoutInSeconds ` option to
166
+ configure this behavior.
112
167
113
168
``` ts
114
169
const response = await beehiiv .subscriptions .create (... , {
@@ -118,23 +173,23 @@ const response = await beehiiv.subscriptions.create(..., {
118
173
119
174
## Runtime compatiblity
120
175
121
- The SDK defaults to ` node-fetch ` but will use the global fetch client if present. The SDK
122
- works in the following runtimes:
176
+ The SDK defaults to ` node-fetch ` but will use the global fetch client if present. The SDK
177
+ works in the following runtimes:
123
178
124
179
The following runtimes are supported:
125
180
126
- - Node.js 15+
127
- - Vercel
128
- - Cloudflare Workers
129
- - Deno v1.25+
130
- - Bun 1.0+
131
- - React Native
181
+ - Node.js 15+
182
+ - Vercel
183
+ - Cloudflare Workers
184
+ - Deno v1.25+
185
+ - Bun 1.0+
186
+ - React Native
132
187
133
188
### Customizing Fetch client
134
189
135
- The SDK provides a way for you to customize the underlying HTTP client / Fetch function. If you're
136
- running in an unsupported environment, this provides a way for you to break the glass and
137
- ensure the SDK works.
190
+ The SDK provides a way for you to customize the underlying HTTP client / Fetch function. If you're
191
+ running in an unsupported environment, this provides a way for you to break the glass and
192
+ ensure the SDK works.
138
193
139
194
``` ts
140
195
import { BeehiivClient } from ' beehiiv' ;
@@ -147,14 +202,17 @@ const beehiiv = new BeehiivClient({
147
202
148
203
## Beta status
149
204
150
- This SDK is in beta, and there may be breaking changes between versions without a major version update.
151
- Therefore, we recommend pinning the package version to a specific version in your package.json file.
152
- This way, you can install the same version each time without breaking changes unless you are
205
+ This SDK is in beta, and there may be breaking changes between versions without a major version update.
206
+ Therefore, we recommend pinning the package version to a specific version in your package.json file.
207
+ This way, you can install the same version each time without breaking changes unless you are
153
208
intentionally looking for the latest version.
154
209
155
210
## Contributing
156
211
157
- While we value open-source contributions to this SDK, this library is generated programmatically.
158
- Additions made directly to this library would have to be moved over to our generation code,
159
- otherwise they would be overwritten upon the next generated release. Feel free to open a
160
- PR as a proof of concept, but know that we will not be able to merge it as-is.
212
+ While we value open-source contributions to this SDK, this library is generated programmatically.
213
+ Additions made directly to this library would have to be moved over to our generation code,
214
+ otherwise they would be overwritten upon the next generated release. Feel free to open a PR as
215
+ a proof of concept, but know that we will not be able to merge it as-is. We suggest opening
216
+ an issue first to discuss with us!
217
+
218
+ On the other hand, contributions to the README are always very welcome!
0 commit comments