Node.js Wrapper for the SteamGridDB API https://www.steamgriddb.com/api/v2
Find a file
2020-01-14 01:29:16 +08:00
src whoops put it back 2020-01-14 01:28:36 +08:00
test Initial commit 2019-06-19 03:35:57 +08:00
.eslintrc.json Initial commit 2019-06-19 03:35:57 +08:00
.gitignore Initial commit 2019-06-19 03:35:57 +08:00
LICENSE.md Initial commit 2019-06-19 03:35:57 +08:00
package-lock.json 1.4.0 2020-01-14 01:29:16 +08:00
package.json 1.4.0 2020-01-14 01:29:16 +08:00
README.md Return response object with error 2019-07-26 17:33:21 +08:00

SteamGridDB for Node.js

npm license license
A Node.js wrapper for the SteamGridDB API.

Installation

npm install steamgriddb

Getting Started

Get your API key

You can generate an API key on the preferences page.

Require the library into your project.

const SGDB = require('steamgriddb');

Pass your API key into the constructor:

const client = new SGDB('your_api_key');

Optionally, you can pass an object with some settings:

const client = new SGDB({
  key: 'your_api_key',
  // Array of headers to send with each request
  headers: {
    'X-Some-Header': 'Some Value',
  },
  baseURL: 'https://www.steamgriddb.com/api/v2'
});

Search for a game:

client.searchGame('Half-Life 2')
    .then((output) => {
        console.log(output);
    })
    .catch((err) => {
        console.log(err);
    });

That outputs:

[
  {
    "id": 2254,
    "name": "Half-Life 2",
    "types": [
      "steam"
    ],
    "verified": true
  },
  {
    "id": 21207,
    "name": "Half-Life",
    "types": [
      "steam"
    ],
    "verified": true
  },
  {
    "id": 1417,
    "name": "Half-Life: Blue Shift",
    "types": [
      "steam"
    ],
    "verified": true
  },
  {
    "id": 3868,
    "name": "Half-Life: Source",
    "types": [
      "steam"
    ],
    "verified": true
  }
]

Get some grids:

// Get grids by game ID
client.getGrids({type: 'game', id: 2254}) // 2254 = Game ID we got from searchGame()
    .then((output) => {
        console.log(output);
    })
    .catch((err) => {
        console.log(err);
    });
    
// Get grid by Steam App Id
client.getGrids({type: 'steam', id: 220}) // 220 = https://store.steampowered.com/app/220/HalfLife_2/
    .then((output) => {
        console.log(output);
    })
    .catch((err) => {
        console.log(err);
    });
    
// Alternatively, you can do it like this:
client.getGridsById(2254)
    .then((output) => {
        console.log(output);
    })
    .catch((err) => {
        console.log(err);
    });
    
client.getGridsBySteamAppId(220)
    .then((output) => {
        console.log(output);
    })
    .catch((err) => {
        console.log(err);
    });

Filter the styles:

client.getGrids({type: 'game', id: 2590, styles: ['material','blurred']})
    .then((output) => {
        console.log(output);
    })
    .catch((err) => {
        console.log(err);
    });

// And if you like to do things the other way:
client.getGridsBySteamAppId(220, ['material','blurred'])
    .then((output) => {
        console.log(output);
    })
    .catch((err) => {
        console.log(err);
    });

The getGrid*() methods give us an output like this:

[
  {
    "id": 80,
    "score": 1,
    "style": "blurred",
    "url": "https://s3.amazonaws.com/steamgriddb/grid/f033ab37c30201f73f142449d037028d.png",
    "thumb": "https://s3.amazonaws.com/steamgriddb/thumb/f033ab37c30201f73f142449d037028d.png",
    "tags": [],
    "author": {
      "name": "EpicWolverine",
      "steam64": "76561198025674497",
      "avatar": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/76/764911aeb96ae312c2819780f4107396d3b5ac38.jpg"
    }
  },
  {
    "id": 120,
    "score": 1,
    "style": "blurred",
    "url": "https://s3.amazonaws.com/steamgriddb/grid/da4fb5c6e93e74d3df8527599fa62642.png",
    "thumb": "https://s3.amazonaws.com/steamgriddb/thumb/da4fb5c6e93e74d3df8527599fa62642.png",
    "tags": [],
    "author": {
      "name": "Tiederian",
      "steam64": "76561197997534033",
      "avatar": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/bc/bc893073b7e2e60ad412c6fd2af39d3204a5f26a.jpg"
    }
  }
]

Do something with the grid output:

const SGDB = require('steamgriddb');
const request = require('request');
const fs = require('fs');

const client = new SGDB('my_api_key');
client.getGridsBySteamAppId(220, ['blurred'])
    .then((output) => {
        // Save each Blurred Half-Life 2 grid to disk
        output.forEach((grid) => {
            request(grid.url).pipe(fs.createWriteStream(`grid-${grid.id}.png`));
        });
    });

Other methods

Vote on grids:

// Upvote a grid
client.voteGrid({direction: 'up', id: 80}) // 80 = Grid ID
    .then((res) => {
        console.log(res); // true/false
    });

// Downvote the same grid
client.voteGrid({direction: 'down', id: 80}) // 80 = Grid ID
    .then((res) => {
        console.log(res); // true/false
    });
    
// Alternatively:
client.upvoteGrid(80)
    .then((res) => {
        console.log(res); // true/false
    });
client.downvoteGrid(80)
    .then((res) => {
        console.log(res); // true/false
    });

Upload a grid:

// Upload a blurred grid to Half-Life 2 (2254)
client.uploadGrid(2254, 'blurred', fs.createReadStream(__dirname + '/grid.png'))
    .then((res) => {
        console.log(res);
    })
    .catch((err) => {
        console.log(err);
    });

Delete grids:

// Delete a grid
client.deleteGrids(80)
    .then((res) => {
        console.log(res);
    })
    .catch((err) => {
        console.log(err);
    });

// Delete multiple grids
client.deleteGrids([80,81,82,83])
    .then((res) => {
        console.log(res);
    })
    .catch((err) => {
        console.log(err);
    });

Handling errors:

// Try to delete a grid you don't own
client.deleteGrids(34312)
    .then((res) => {
        console.log(res); // you don't own the grid so this wont happen
    })
    .catch((err) => {
        console.log(err.message); // "This grid isn't yours."

        // If needed, you can access the response object with err.response
        console.log(err.response.statusCode); // 403
    });