Install it in your project:
npm i @jellyfin/client-axios
This library follows the API pattern you can get on https://api.jellyfin.org/ or on your own server at /api-docs/swagger/index.html
or /api-docs/redoc/index.html
.
// Get server infos
import { SystemApi } from "@jellyfin/client-axios";
const systemApi = new SystemApi(
undefined, // OpenAPI configuration object, found in `configuration.ts`
"https://demo.jellyfin.org/stable", // Base URL
undefined // Axios base object to use for queries, can be used to set base URL or headers
);
const infos = (await systemApi.getPublicSystemInfo()).data;
console.log(infos);
For authentication, Jellyfin needs an Authorization
header with a a few informations filled in, and that isn't part of the SDK spec. So the authentication method doesn't pass the Configuration
API key field that we will use later, and we need to specify it manually. It'll be then used to append the fetched authentication token and use it as the classic authorization header.
import { v4 } from "uuid"; // Used to generate random string
import { UserApi } from "@jellyfin/client-axios";
const base_token = `MediaBrowser Client="My New Client", Device="${
window.navigator.userAgent
}", DeviceId="${v4()}", Version="1.0.0"`; // Base string for the full header
const vanilla_token = `${base_token}, Token=""`; // We need a vanilla token to authenticate
const ax = axios.create({
headers: {
Authorization: vanilla_token
}
}); // Prepare the authentification header
const userApi = new UserApi(undefined, "https://demo.jellyfin.org/stable", ax);
const res = (
await userApi.authenticateUserByName({
authenticateUserByName: {
Username: "demo",
Pw: ""
}
})
).data;
const logged_token = `${base_token}, Token="${res.AccessToken}"`; // Re-use our base token to append the newly fetched one
const userId = res.User?.Id;
console.log(logged_token);
console.log(userId);
For other calls, we don't need to use a specific Axios object and just a Configuration
one with our server URL and the full size token.
const conf = new Configuration({
basePath: url,
apiKey: token
}); // Use the OpenAPI configuration object to pass the JF header and server URL
const libraryApi = new LibraryApi(conf);
const libs = (await libraryApi.getMediaFolders()).data;
console.log(libs);
const artistsApi = new ArtistsApi(conf);
const artist = (
await artistsApi.getArtistByName({
name: "Flume"
})
).data; // Each API parameter is typed and available as method's parameters
console.log(artist);