Git Product home page Git Product logo

jelly-docs's Introduction

Coding for Jellyfin API

I'm writing a thing that connects to Jellyfin API and am sharing some details on how to do it.

Thanks to James Harvey for his helpful article that got me started at: https://jmshrv.com/posts/jellyfin-api/

Jellyfin API docs

You can find the swagger for your own Jellyfin instance at http://localhost/api-docs/swagger/index.html. The documentation there likely differs a bit from the official Jellyfin API Docs: https://api.jellyfin.org/

I also recommend you go look at the source code https://github.com/jellyfin/jellyfin/tree/master/Jellyfin.Api

But again, that's from the master branch so it'll likely be newer than your local instance.

Authentication

To authenticate with the Jellyfin API there are a few different options. The easy one is /Users/AuthenticateByName.

This endpoint is POST and takes an authorization header that has 4 pieces of information:

  • Client: The name of your client
  • Version: Version number of your client
  • Device: The name of the device
  • DeviceId: An identifier for the current device

It also takes your username and password as the POST content.

const api = axios.create({ baseURL: 'http://localhost:8096' });

const deviceName = 'MyDevice'
const deviceId = 'SomeGuid' // Potentially generate one and save in local storage
const clientName = 'NameOfYourClientApplication'
const version = '0.0.1' // Version of your client application

const response = await api.post(
  '/Users/AuthenticateByName',
  {
    Username: 'bob',
    Pw: 'PlainTextIn2024?'
  },
  {
    headers: {
      // It is critical this header starts with 'MediaBrowser', Jellyfin expects it.
      Authorization: `MediaBrowser Client="${clientName}", Device="${deviceName}", DeviceId="${deviceId}", Version="${version}"`
    }
  }) 

// Grab UserId (needed for many other calls)
const userId = response.data?.User?.Id;
const token = response.data?.AccessToken;

// You can make the header default for future calls with axios like this:
api.defaults.headers.common['Authorization'] = `MediaBrowser Client="${clientName}", Device="${deviceName}", DeviceId="${deviceId}", Version="${version}" Token="${token}"`;

Getting items from the API

Items in Jellyfin are all of a gigantic basetype called BaseItemDto

You can access instances of this type through endpoints such as /Users/${userId}/Items.

I'm building a simple music player so I just want all the music on my server:

const itemsResponse = await api.get(`/Users/${userId}/Items`, {
  params: {
    IncludeItemTypes: 'Audio',
    recursive: true,
  },
});

const allSongs = itemResponse.data.Items

Playing music

Again since I'm building a music player, I need a URL to pass to an AudioElement.

To do that I access /Audio/${item.Id}/universal.

Since you cannot pass headers to an audio element src tag. We pass the token using a query parameter.

const item = allsongs[0]
audioElement.src = `${api.getUri()}/Audio/${item.Id}/universal?ApiKey=${token}`

jelly-docs's People

Contributors

felizk avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.