# twitch-helix

> Little helper class for the new Twitch Helix API

Latest version **1.0.7** (published 2017-12-30) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install twitch-helix
pnpm add twitch-helix
yarn add twitch-helix
bun add twitch-helix
```

Provides the command `twitch-helix`.

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.0.7 |
| Published | 2017-12-30 |
| First published | 2017-10-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=8 |
| Dependencies | 7 |
| Known vulnerabilities | 0 (+2 in 2 direct dependencies) |
| Install scripts | no |
| Author | Jaid |
| Maintainers | jaid |

## Links

- npm: https://www.npmjs.com/package/twitch-helix
- Repository: https://github.com/jaid/twitch-helix
- Homepage: https://github.com/jaid/jaid-web#readme
- Issues: https://github.com/jaid/jaid-web/issues
- npm.io page: https://npm.io/package/twitch-helix

## Dependencies (7)

- [lodash](https://npm.io/package/lodash.md) ^4.17.4
- [request](https://npm.io/package/request.md) ^2.83.0
- [winston](https://npm.io/package/winston.md) ^2.4.0
- [commander](https://npm.io/package/commander.md) ^2.12.2
- [prettyjson](https://npm.io/package/prettyjson.md) ^1.2.1
- [requestretry](https://npm.io/package/requestretry.md) ^1.12.2
- [babel-runtime](https://npm.io/package/babel-runtime.md) ^6.26.0

## Recent versions

- 1.0.7 (latest) — 2017-12-30
- 1.0.6 — 2017-12-18
- 1.0.5 — 2017-12-10
- 1.0.4 — 2017-12-06
- 1.0.3 — 2017-11-30
- 1.0.2 — 2017-11-12
- 1.0.1 — 2017-11-04
- 1.0.0 — 2017-10-30
- 0.4.3 — 2017-10-29
- 0.4.2 — 2017-10-18
- 0.4.1 — 2017-10-18
- 0.3.4 — 2017-10-16
- 0.3.3 — 2017-10-16
- 0.3.1 — 2017-10-14
- 0.3.0 — 2017-10-14
- … 9 more at https://npm.io/package/twitch-helix/versions

## README

# twitch-helix
Little helper class for the new Twitch API described in current [Twitch API docs](https://dev.twitch.tv/docs/api/reference).<br>
[Transpiled and minified](https://unpkg.com/twitch-helix) with Babel.

[![npm Stats](https://nodei.co/npm/twitch-helix.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/twitch-helix/)

[![Travis Build Status](https://api.travis-ci.org/Jaid/twitch-helix.svg)](https://travis-ci.org/Jaid/twitch-helix)
[![Dependency Status](https://gemnasium.com/badges/github.com/Jaid/twitch-helix.svg)](https://gemnasium.com/github.com/Jaid/twitch-helix)
[![MIT License](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/Jaid/twitch-helix/master/license.txt)

Feel free to contribute by creating [issues](https://github.com/Jaid/twitch-helix/issues) and [pull requests](https://github.com/Jaid/twitch-helix/pulls).

## Installation

Yarn (recommended)
```bash
yarn add twitch-helix
```

npm
```bash
npm install --save twitch-helix
```

## Library Usage

#### Example

Import the default class from this package and feed its constructor with a client ID and a client secret. You can generate those in your [Twitch Developers Dashboard](https://dev.twitch.tv/dashboard/apps).<br>
Try it out on [RunKit](https://npm.runkit.com/twitch-helix)!

```jsx
import TwitchHelix from "twitch-helix"
// Or: const TwitchHelix = require("twitch-helix")

const twitchApi = new TwitchHelix({
    clientId: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    clientSecret: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
})

twitchApi.getTwitchUserByName("nightbot").then(twitchUser => {
    console.log(twitchUser.display_name) // Prints "Nightbot"
})
```

#### Construction

TwitchHelix is a class and you need to create an instance with:

```jsx
const twitchApi = new TwitchHelix(options)
```

The options parameter is an object and can have following fields:

Field|Info|Default value
---|---|---
`clientId`|Client ID of your Twitch app|:no_entry_sign: (required)
`clientSecret`|Client secret of your Twitch app|:no_entry_sign: (required)
`prematureExpirationTime`|Time in ms for the access token to expire before it is meant to (Not implemented yet)|`10000`
`autoAuthorize`|Will call automatically call authorize() when needed|`true`
`smartRetry`|Will retry Twitch API requests up to 10 times if the server response is invalid|`true`

#### Implemented queries

Some of the common queries are wrapped into neat functions. Those are:

Promise|Name|Parameters|Return Value|Further info
---|---|---|---|---
Yes|`getTwitchUserById`|string `id`|Twitch user info object
Yes|`getTwitchUserByName`|string `username`|Twitch user info object
Yes|`getTwitchUsersByName`|Array<string> `usernames`|Array of Twitch user info objects
Yes|`getStreamInfoById`|string `id`|Twitch stream object if user is currently streaming or `null` otherwise
Yes|`getStreamInfoByUsername`|string `username`|Twitch stream object if user is currently streaming or `null` otherwise
Yes|`getFollowDate`|string `streamerId`, string `followerId`|`Date` if follower follows streamer or `null` otherwise

#### Custom queries

You may need custom queries for retrieving data that is not wrapped into a function yet. Feel free to do so. Some API endpoints are still not implemented by Twitch in Helix API, so you can also use [Kraken v5 API](https://dev.twitch.tv/docs/v5).

```jsx
const helixQueryData = await twitchApi.sendHelixRequest("users?login=nightbot&login=moobot")
const krakenQueryData = await twitchApi.sendApiRequest("users?login=nightbot,moobot", {api: "kraken"})
```

#### Events

You can listen to some events.
```jsx
twitchApi.on(eventName, eventHandler)
```

Event name|Parameters|Description
---|---|---
`log-info`|`message`|Emitted on INFO log messages
`log-warn`|`message`|Emitted on WARN log messages
`log-error`|`message`|Emitted on ERROR log messages

## Command Line Usage

Here is an example:
```bash
node_modules/.bin/twitch-helix --client-id xxx --client-secret xxx "users?login=nightbot"
``` 

This will print:
[![Command Line Output](https://i.imgur.com/PTdBOQW.png)](https://i.imgur.com/PTdBOQW.png)

Use the `--kraken` flag to query data from a Kraken endpoint instead of a Helix endpoint.

---
_Source: https://npm.io/package/twitch-helix · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
