npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

torn-api-wrapper

v0.4.21

Published

A wrapper to integrate with the Torn API. Completely type-safe.

Downloads

59

Readme

Torn API Wrapper

A wrapper to integrate with the Torn API. Completely type-safe.

Table of Contents

  1. Getting Started
  2. Installing
  3. Using the wrapper
    1. Initial idea
    2. Error handling
    3. TornAPI
      1. addKey(key: string)
      2. removeKey(key: string)
      3. checkIfKeyIsValid(key: string): Promise<number | null>
    4. Market
      1. getLowestListing(itemId: number)
      2. Bazaar
        1. getItems(itemId: number, limit?: number)
      3. Itemmarket
        1. getItems(itemId: number, limit?: number)
      4. Pointsmarket
        1. getPoints()
        2. getPointsWithoutIds()
    5. Torn
      1. Items
        1. getItems(itemId?: number[])
        2. getItemValue(itemId: number)
  4. Versioning
  5. Authors
  6. Acknowledgments

Getting Started

In this section I will show you how to install and use the wrapper. It is possible to pass one apiKey:string or apiKey[] to the constructor. A random key is selected from the array. If you pass an array of keys, the wrapper will automatically remove the key if the current key is invalid.

const TornApi = require('torn-api-wrapper').default

const api = new TornApi('YOUR_API_KEY') // Using a single key
const apiPool = new TornApi(['KEY1', 'KEY2', 'KEY3']) // Using a pool of keys

const getBazaarItems = async () => {
  const bazaar = await api.market.bazaar.getItems(206)
  
  if (bazaar) {
    console.log(bazaar)
  } else {
    console.log(api.error)
  }
}

getBazaarItems()

Installing

Install the wrapper as a dev dependency via NPM or yarn.

npm i torn-api-wrapper -d
yarn add torn-api-wrapper -D

Using the wrapper

Initial idea

The wrapper was built to be as close as possible to the Torn Api, making development as intuitive as possible. One of the 7 sections is always called up first, followed by subcategories and then helper functions.

const market = await api.market.itemmarket.getItems(206)
const bazaar = await api.market.bazaar.getItems(206)

Error handling

The main class api contains a error property. This property is updated with every request. If an error occurs, the error is stored in the error property. The error object contains the following properties: code and message. The code property is the error code of the Torn API. The message property is the error message of the Torn API. The requested data is null if an error occurs.

const bazaar = await api.market.bazaar.getItems(206)

// Way 1 - With error handling
if (api.error) {
    console.log(api.error)
    // { code: 2, message: 'Incorrect key', apiKey: 'YOUR_API_KEY' }
} else {
    console.log(bazaar)
}

// Way 2 - No error handling
if (bazaar) {
    console.log(bazaar)
}

TornAPI

addKey(key: string)

Adds a key to the key pool

api.addKey('YOUR_API_KEY')

removeKey(key: string)

Removes a key from the key pool

api.removeKey('YOUR_API_KEY')

checkIfKeyIsValid(key: string): Promise<number | null>

Checks if a key is valid. Returns the key level if it is valid, otherwise null

await api.checkIfKeyIsValid('YOUR_API_KEY')

if (api.error) {
    console.log(api.error)
    // { code: 2, message: 'Incorrect key' }
} else {
    console.log(bazaar)
}

Market

getLowestListing(itemId: number)

Returns the lowest listing for the given itemId. Possible types: bazaar, itemmarket

const lowestListing = await api.market.getLowestListing(206)
// { type: 'bazaar', cost: 842495, quantity: 6, total_cost: 5054970 }

Bazaar

getItems(itemId: number, limit?: number)

Returns a list of bazaar listings for the given itemId

const bazaar = await api.market.bazaar.getItems(206)

// [
//   { ID: 73075318, cost: 837000, quantity: 50 },
//   { ID: 49697817, cost: 842700, quantity: 2 },
//   ...
// ]

Itemmarket

getItems(itemId: number, limit?: number)

Returns a list of itemmarket listings for the given itemId

const itemmarket = await api.market.itemmarket.getItems(206)

// [
//   { ID: 203569084, cost: 840000, quantity: 1 },
//   { ID: 203569089, cost: 840000, quantity: 1 },
//   ...
// ]

Pointsmarket

getPoints()

Returns a object of points market items with there ids

const pointsmarket = await api.market.pointsmarket.getPoints()

// {
//   '14686258': { cost: 45870, quantity: 25, total_cost: 1146750 },
//   '14686272': { cost: 45850, quantity: 2000, total_cost: 91700000 },
//   ...
// }

getPointsWithoutIds()

Returns a object of points market items with there ids

const pointsmarket = await api.market.pointsmarket.getPoints()

// [
//     { cost: 45870, quantity: 25, total_cost: 1146750 },
//     { cost: 45870, quantity: 25, total_cost: 1146750 },
//     ...
// ]

Torn

Items

getItems(itemId?: number[])

Retrieves the details of an item

const itemDetails = await api.torn.items.getItemDetails([206, 207])

// {
//   "206": {
//     "name": "Xanax",
//     "description": "Increases one's energy.",
//     "effect": "Increases energy by 250 and happiness by 75. Includes side effects.",
//     "requirement": "",
//     "type": "Drug",
//     "weapon_type": null,
//     "buy_price": 0,
//     "sell_price": 0,
//     "market_value": 841051,
//     "circulation": 4841871,
//     "image": "https://www.torn.com/images/items/206/large.png"
//   },
//   "207": {
//     "name": "Ms Torn Crown '07",
//     "description": "Awarded to Vixen_ [140202] for winning the Miss Torn City awards 2007!",
//     "effect": "",
//     "requirement": "",
//     "type": "Collectible",
//     "weapon_type": null,
//     "buy_price": 0,
//     "sell_price": 0,
//     "market_value": 0,
//     "circulation": 1,
//     "image": "https://www.torn.com/images/items/207/large.png"
// }

getItemValue(itemId: number)

Retrieves the value of an item

const itemValue = await api.torn.items.getItemValue(206)

// 842652

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Authors

See also the list of contributors who participated in this project.

Acknowledgments

  • Inspired by Jgollas Torn API Package - Torn API
  • Thank you for the extensive input from TimbowSix