File size: 5,497 Bytes
95f4e64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# Getting Started

A NodeJS SDK for making and publishing Stremio addons

## Example

**This arbitrary example creates an addon that provides a stream for Big Buck Bunny and outputs a HTTP address where you can access it**

```javascript
#!/usr/bin/env node

const { addonBuilder, serveHTTP, publishToCentral } = require('stremio-addon-sdk')

const builder = new addonBuilder({
    id: 'org.myexampleaddon',
    version: '1.0.0',

    name: 'simple example',

    // Properties that determine when Stremio picks this addon
    // this means your addon will be used for streams of the type movie
    resources: ['stream'],
    types: ['movie'],
    idPrefixes: ['tt']
})

// takes function(args), returns Promise
builder.defineStreamHandler(function(args) {
    if (args.type === 'movie' && args.id === 'tt1254207') {
        // serve one stream to big buck bunny
        // return addonSDK.Stream({ url: '...' })
        const stream = { url: 'http://distribution.bbb3d.renderfarming.net/video/mp4/bbb_sunflower_1080p_30fps_normal.mp4' }
        return Promise.resolve({ streams: [stream] })
    } else {
        // otherwise return no streams
	return Promise.resolve({ streams: [] })
    }
})

serveHTTP(builder.getInterface(), { port: 7000 })

// If you want this addon to appear in the addon catalogs, call .publishToCentral() with the publically available URL to your manifest
//publishToCentral('https://my-addon.com/manifest.json')

```

Save this as `addon.js` and run:

```bash
npm install stremio-addon-sdk
node ./addon.js
```

It will output a URL that you can use to [install the addon in Stremio](./docs/testing.md#how-to-install-add-on-in-stremio)

## Documentation

**To get familiar with the resources and their roles, [read this](./api/README.md).**

#### `const { addonBuilder, serveHTTP, getRouter, publishToCentral } = require('stremio-addon-sdk')`

Imports everything the SDK provides:

* `addonBuilder`, which you'll need to define the addon
* `serveHTTP`, which you will need to serve a HTTP server for this addon
* `getRouter`, converts an `addonInterface` to an express router
* `publishToCentral`: publishes an addon URL to the public addon catalog


#### `const builder = new addonBuilder(manifest)`

Creates an addon builder object with a given manifest. This will throw if the manifest is not valid.

The manifest will determine the basic information of your addon (name, description, images), but most importantly, it will determine **when and how** your addon will be invoked by Stremio.

[Manifest Object Definition](./api/responses/manifest.md)


#### `builder.defineCatalogHandler(function handler(args) { })`

Handles catalog requests, including search.

[Catalog Request Parameters and Example](./api/requests/defineCatalogHandler.md)


#### `builder.defineMetaHandler(function handler(args) { })`

Handles metadata requests. (title, releaseInfo, poster, background, etc.)

[Meta Request Parameters and Example](./api/requests/defineMetaHandler.md)


#### `builder.defineStreamHandler(function handler(args) { })`

Handles stream requests.

[Stream Request Parameters and Example](./api/requests/defineStreamHandler.md)


#### `builder.defineSubtitlesHandler(function handler(args) { })`

Handles subtitle requests.

[Subtitle Request Parameters and Example](./api/requests/defineSubtitlesHandler.md)


#### `builder.defineResourceHandler('addon_catalog', function handler(args) { })`

Handles addon catalog requests, this can be used by an addon to just send a list of other addon manifests.

[Addon Catalog Request Parameters and Example](./api/requests/defineResourceHandler.md)

**The JSON format of the response to these resources is described [here](./api/responses).**


#### `builder.getInterface()`: returns an `addonInterface`

Turns the `addon` into an `addonInterface`, which is an immutable (frozen) object that has `{manifest, get}`; manifest is a regular [manifest object](./api/responses/manifest.md), while `get` is a function that takes one argument of the form `{ resource, type, id, extra }`, and returns a `Promise`


#### `getRouter(addonInterface)`

Turns the `addonInterface` into an express router, that serves the addon according to [the protocol](./protocol.md), and a landing page on the root (`/`)


#### `publishToCentral(url)`

This method expects a string with the url to your `manifest.json` file.

Publish your addon to the central server. After using this method your addon will be available in the Community Addons list in Stremio for users to install and use. Your addon needs to be publicly available with a URL in order for this to happen, as local addons that are not publicly available cannot be used by other Stremio users.


#### `serveHTTP(addonInterface, options)`

Starts the addon server. `options` is an object that contains:

* `port`
* `cacheMaxAge` (in seconds); `cacheMaxAge` means the `Cache-Control` header being set to `max-age=$cacheMaxAge`
* `static`: path to a directory of static files to be served; e.g. `/public` 

This method is also special in that it will react to certain process arguments, such as:

* `--launch`: launches Stremio in the web browser, and automatically installs/upgrades the addon
* `--install`: installs the addon in the desktop version of Stremio


#### `addonInterface`

The `addonInterface`, as returned from `builder.getInterface()`, has two properties:

* `get({ resource, type, id, extra })` - returns a Promise
* `manifest`: [manifest object](./api/responses/manifest.md)