substack-sdk
v1.1.0
Published
substack unofficial SDK
Downloads
10
Readme
Substack SDK
Motivation
Provide an typescript async promise based unofficial sdk for substack platform.
Installation
add it to your project using npm install substack-sdk --save
or yarn add substack-sdk
Usage
this sdk provide functions realated to various substack objects
Authorization
Before any use you will require to initialize this SDK by copying the cookie to initialize any type of transaction.
import substack from "substack-sdk"
const cookie = "your substack cookie"
const domain = "your substack domain" // optional in most cases, but required for things like subscribers
substack.init(cookie, options)
Images
Upload
to upload an image you need to pass the base64 representation of it to the image.upload
method. As seen on the next example:
import substack from "substack-sdk"
const imageString = `base64 representation of the image
ex: data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAk0AAAJUCAIAAACHQYVcAAAACXBIWXMAAAsTAAALEwEAmpwYAAAgAElEQVR4nOy9WXMc130+fLpn3/...`
const uploadImageRespose = await substack.image.upload(imageString)
Attachemnts
add
Attachments are used as references to files (uploaded) and links that will be referenced in other objects we will se further on in this library.
The first parameter will be the type of the attachment (image
or link
), that will be the url passed in the secont parameter.
import substack from "substack-sdk"
const attachmentResponse = await substack.attachment.create("image",uploadImageRespose.url)
const attachmentResponse2 = await substack.attachment.create("link","https://www.alvarolorente.dev/blog/2024/02/14/leaders-build-trust-managers-take-control-the-path-to-empowering-teams")
Notes
Add
This is the simplest type of user interactive object, where you can use the attachments defined in the previous section.
The first parameter is an array of contents of the next types:
type Text = {
message: string
}
type Link = {
url: string
text: string
}
type Content = Link | Text
The second parameter and will be an array of the attachment ids that whant to be link in this Note
import substack from "substack-sdk"
const createNoteRespose = await substack.note.create([
{ message: "Hello World" },
{
text: "awesome post",
url: "https://www.alvarolorente.dev/blog/2024/02/14/leaders-build-trust-managers-take-control-the-path-to-empowering-teams"
}
], [attachmentResponse.id, attachmentResponse2.id])
Subscribers
This provides you access to the paginated set of your subscribers. This will require you to initialize the api with the correct domain of your newsletter.
This endpoint is paginated so you will need to pass the number of records to retrieve and the offset as second parameter.
import substack from "substack-sdk"
await substack.subscribers.retrieve(50,0)
Roadmap
- [x] Images
- [x] Notes
- [x] Attachments
- [ ] Threads
- [x] Subscribers
- [ ] Articles
- ...