steamid-ts
v0.1.0
Published
Parse, validate, and convert Steam IDs and Steam trade offer URLs.
Maintainers
Readme
steamid-ts
steamid-ts is a small TypeScript library for parsing, validating, and
converting Steam identifiers. It works in Node.js and modern browsers, has no
runtime dependencies, and includes first-class support for Steam trade offer
URLs.
Features
- Parse SteamID64, Steam2, Steam3, numeric profile URLs, invite URLs, and trade offer URLs.
- Convert between SteamID64, Steam2, Steam3, and account IDs.
- Extract trade URL data, including the partner account ID, derived SteamID64, trade token, and extra query parameters.
- Resolve vanity URLs through a caller-provided resolver, so API keys stay on your backend.
- Ship ESM, CommonJS, and TypeScript declaration builds.
Installation
npm install steamid-tsQuick Start
import { SteamID } from 'steamid-ts';
const steamID = new SteamID('76561197984981409');
const steam3 = steamID.toSteam3();
const steam2 = steamID.toSteam2();
const uint64 = steamID.toUInt64();Results:
| Value | Output |
| -------- | -------------------- |
| steam3 | [U:1:24715681] |
| steam2 | STEAM_1:1:12357840 |
| uint64 | 76561197984981409 |
Supported Inputs
new SteamID('76561197984981409');
new SteamID('STEAM_0:1:4491990');
new SteamID('[U:1:24715681]');
new SteamID('[g:1:4145017]');URLs that contain enough information locally can also be parsed without a backend lookup:
const resolver = () => null;
SteamID.fromURL(
'https://steamcommunity.com/profiles/76561197984981409',
resolver,
);
SteamID.fromURL('https://s.team/p/qpn-pmn', resolver);
SteamID.fromURL(
'https://steamcommunity.com/tradeoffer/new/?partner=177628825&token=RTL-Qm-w',
resolver,
);Trade URLs
Trade offer URLs encode the partner query parameter as the Steam account ID
from Steam3 format. For example, partner=177628825 maps to
[U:1:177628825] and 76561198137894553.
Use fromTradeURL when you need the trade token or other query parameters:
import { SteamID } from 'steamid-ts';
const trade = SteamID.fromTradeURL(
'https://steamcommunity.com/tradeoffer/new/?partner=177628825&token=RTL-Qm-w',
);
const accountID = trade.accountID;
const steam3 = trade.steamID.toSteam3();
const steamID64 = trade.steamID.toUInt64();
const token = trade.token;Results:
| Value | Output |
| ----------- | ------------------- |
| accountID | 177628825 |
| steam3 | [U:1:177628825] |
| steamID64 | 76561198137894553 |
| token | RTL-Qm-w |
fromURL also accepts trade URLs and returns the derived SteamID directly.
Vanity URLs
Steam vanity URLs, such as https://steamcommunity.com/id/xpaw, do not contain
a numeric SteamID. They need a resolver function:
import { SteamID, VanityType, type VanityResolver } from 'steamid-ts';
const resolver: VanityResolver = (id, type) => {
if (id === 'xpaw' && type === VanityType.Individual) {
return '76561197972494985';
}
return null;
};
const steamID = SteamID.fromURL(
'https://steamcommunity.com/id/xpaw/',
resolver,
);Use fromURLAsync if the resolver calls your backend:
const steamID = await SteamID.fromURLAsync(
'https://steamcommunity.com/id/xpaw/',
async (id, type) => {
const response = await fetch(`/api/steam/resolve?id=${id}&type=${type}`);
const data = (await response.json()) as { steamID: string | null };
return data.steamID;
},
);API
new SteamID(value?)
Creates a SteamID from a SteamID64 string, Steam2 string, Steam3 string, number, or bigint.
Instance Methods
isValid()toSteam2()toSteam3()toSteamInvite()toUInt64()toString()setFromUInt64(value)getAccountID()getAccountInstance()getAccountType()getAccountUniverse()setAccountID(value)setAccountInstance(value)setAccountType(value)setAccountUniverse(value)
Static Methods
SteamID.fromAccountID(accountID)SteamID.accountIDToUInt64(accountID)SteamID.renderAccountID(accountID)SteamID.fromURL(value, resolver)SteamID.fromURLAsync(value, resolver)SteamID.fromTradeURL(value)
Validation
Invalid input throws SteamIDError. The parser rejects unsafe or ambiguous
inputs such as whitespace-padded IDs, newline-terminated IDs, Unicode digits,
null bytes, and overflowing values.
Development
npm install
npm test
npm run coverage
npm run typecheck
npm run lint
npm run buildAcknowledgements
This project is a from-scratch TypeScript implementation informed by the
behavior of the MIT-licensed
xPaw/SteamID.php library.
License
MIT
