Node.js API Wrapper for Saweria.co
Check the Changelog
npm i saweria
const { Client } = require("saweria");
// or
import { Client } from "saweria";
const client = new Client();
client.on("login", (user) => {
console.log("Logged in as: ", user.username);
});
client.on("donations", (donations) => {
console.log(donations);
});
client.login("email", "password");
// or with otp
client.login("email", "password", "otp");
or only for donation event listener
const { Client } = require("saweria");
// or
import { Client } from "saweria";
const client = new Client();
client.setStreamKey("your-stream-key");
client.on("donations", (donations) => {
console.log(donations);
});
Login to Saweria using JWT or email and password. If the account has no 2FA enabled, otp
will be ignored.
This will set the default header authorization value for the future requests with user's JWT
Login with JWT is useful if your account has 2FA enabled and don't want to get your 2FA token every time you login. JWT generated by Saweria lasts for 72 hours, so you have to get a new JWT for your account every 72 hours. You can get your JWT by:
- Using this library
- Log in to your account using
client.login(username, password)
- Get the account JWT from
client.jwt
- Log in to your account using
- Using the web client
- Go to saweria.co and login to your account
- Open browser developer console, and execute
console.log(JSON.parse(localStorage["saweria-user-info"]).token);
- Sending a request to login endpoint
- Send a
POST
request tohttps://api.saweria.co/auth/login
with these JSON body payload:
{ "email": "[email protected]", "password": "your-password", "otp": "your otp code, optional if your account doesn't have 2FA enabled" }
- Get the JWT token from
Authorization
response header.
- Send a
Removes authorization header from HTTP client, preventing future requests.
Set the client's stream key, this can be used to listen to donation event without logging in.
Get user's stream key
Get user profile information
Get user's balance
Get user's available balance to disburse
Get user's transaction list. Accepts these parameters:
page
: What page of transaction to get (Default =1
)pageSize
: How many transactions per page (Default =15
)
Get milestone progress from passed date with dd-mm-yyyy
format until now.
Get donation leaderboard from given time period.
period
can be "all"
, "year"
, "month"
, or "week"
(Default = "all"
)
Get currently active vote data
Send a fake donation
Listen to client events and execute callback
when emitted
Emitted when client successfully logged in. Callback accepts User
as the first parameter
Example:
client.on("login", (user) => {
console.log("Logged in as: ", user.username);
});
Emitted when client received a donation. Callback accepts array of EmittedDonation
or EmittedMedia
as the first parameter
Example:
client.on("donations", (donations) => {
console.log(donations); // array of donations
for (const donation of donations) {
if (donation.type === "normal") {
// normal donation
} else if (donation.type === "media") {
// donation with media
}
}
});
Normal Donation data example:
[
{
amount: "69420",
donator: "Someguy",
media: {
src: [
"https://media2.giphy.com/media/gw3IWyGkC0rsazTi/giphy.webp",
"https://media2.giphy.com/media/gw3IWyGkC0rsazTi/giphy.mp4",
"https://media2.giphy.com/media/gw3IWyGkC0rsazTi/giphy.gif",
],
tag: "picture",
},
message: "THIS IS A FAKE MESSAGE! HAVE A GOOD ONE",
sound: {
"1547679809default.ogg":
"https://saweria-space.sgp1.cdn.digitaloceanspaces.com/prd/sound/836d7a85-dd70-4028-85fb-00fd785f0928-c527b4f6bd6282e21e78c85343d496fa.ogg",
},
tts: "...",
},
];
Media Donation data example:
[
{
amount: "69420",
donator: "Someguy",
media: { end: 10, id: "RRKPkwBkz_0", start: 0, type: "yt" },
message: "THIS IS A FAKE MESSAGE! HAVE A GOOD ONE",
sound: null,
},
];
Similar to donations
, but emits donation one by one when donations are received