DotLottie Class
API reference for the DotLottie class in dotlottie-io — load, mutate, query, and serialize a .lottie package.
DotLottie Class
The core class representing a loaded or built .lottie package. All methods are synchronous.
Import:
const { DotLottie } = require("@lottiefiles/dotlottie-io");Static methods
DotLottie.fromFile(path, password?)
Load a .lottie file directly from a filesystem path.
Parameters:
path:string— the path to the.lottiefile.password?:string— pass to open a password-protected archive.
Returns:
DotLottieExample:
const dl = DotLottie.fromFile("package.lottie");
DotLottie.fromBytes(data, password?)
Load a .lottie file from raw bytes.
Parameters:
data:Buffer— e.g. the result offs.readFileSync.password?:string— pass to decrypt a password-protected archive.
Returns:
DotLottieExample:
const dl = DotLottie.fromBytes(readFileSync("package.lottie"));
Serialization
toBytes(password?)
Serialize the package to a .lottie ZIP Buffer.
Parameters:
password?:string— pass to produce an AES-256 encrypted archive.
Returns:
BufferExample:
writeFileSync("output.lottie", dl.toBytes());
Animations
addAnimation(id, jsonData, options?)
Add an animation from Lottie JSON. Automatically extracts embedded base64 assets and rewrites their references.
Parameters:
id:string— unique identifier for this animation.jsonData:Buffer | string— the Lottie JSON.options?:AnimationOptions
Returns:
void
removeAnimation(id)
Parameters:
id:stringReturns:
voidThrows: if
iddoesn't exist.
getAnimationJson(id)
Parameters:
id:stringReturns:
string | null— the raw Lottie JSON, ornullif not found.
animationIds()
Returns:
string[]
See Managing Animations for a walkthrough.
Themes
addTheme(id, name, jsonData)
Parameters:
id:stringname:string | null— optional display name.jsonData:Buffer | string
Returns:
void
removeTheme(id)
Returns:
voidThrows: if
iddoesn't exist.
getThemeJson(id)
Returns:
string | null
themeIds()
Returns:
string[]
See Managing Themes.
State machines
addStateMachine(id, name, jsonData)
Parameters:
id:stringname:string | null— optional display name.jsonData:Buffer | string
Returns:
void
removeStateMachine(id)
Returns:
voidThrows: if
iddoesn't exist.
getStateMachineJson(id)
Returns:
string | null
stateMachineIds()
Returns:
string[]
Assets
All add* methods return the actual filename stored, which may differ from the requested one on a collision — see filename deduplication.
addImage(filename, data) / getImage(filename) / imageFilenames()
addImage(filename: string, data: Buffer): stringgetImage(filename: string): Buffer | nullimageFilenames(): string[]
addFont(filename, data) / getFont(filename) / fontFilenames()
addFont(filename: string, data: Buffer): stringgetFont(filename: string): Buffer | nullfontFilenames(): string[]
addAudio(filename, data) / getAudio(filename) / audioFilenames()
addAudio(filename: string, data: Buffer): stringgetAudio(filename: string): Buffer | nullaudioFilenames(): string[]Throws:
InvalidAudioFormatiffilenameisn't.mp3.
Example:
const storedName = dl.addImage("logo.png", readFileSync("logo.png"));
console.log(storedName); // 'logo.png' (or 'logo_1.png' if that name was taken)See Managing Assets.
Query methods
animationsUsingTheme(themeId)
Returns:
string[]— animation IDs referencing the given theme.
animationsUsedByStateMachine(stateMachineId)
Returns:
string[]— unique animation IDs referenced by the state machine'sPlaybackStateentries.
assetReferences(animationId)
Returns:
AssetReferences—{ images: string[], fonts: string[], audio: string[] }.
animationsUsingAsset(filename)
Returns:
string[]— animation IDs that reference the given asset filename.
See Querying Relationships for worked examples.
Manifest
getManifestJson()
Returns:
string— the full manifest as JSON.
setInitial(animationId?, stateMachineId?)
Parameters:
animationId?:string | nullstateMachineId?:string | null
Returns:
voidPass
nullto clear either field.
getInitialAnimationId()
Returns:
string | null
getInitialAnimation()
Returns:
string | null— the raw Lottie JSON of the initial animation.
See The Manifest.
Complete example
const { readFileSync, writeFileSync } = require("node:fs");
const { DotLottie, DotLottieBuilder } = require("@lottiefiles/dotlottie-io");
// Build a package from scratch
const builder = new DotLottieBuilder();
builder.generator("my-tool");
builder.addAnimation("hero", JSON.stringify(myLottieJson));
builder.addTheme("dark", "Dark Theme", JSON.stringify({ rules: [] }));
const dl = builder.build();
// Add assets
const storedName = dl.addImage("logo.png", readFileSync("logo.png"));
dl.addAudio("click.mp3", readFileSync("click.mp3"));
// Save to disk
writeFileSync("output.lottie", dl.toBytes());
// Load back and inspect
const loaded = DotLottie.fromFile("output.lottie");
console.log(loaded.animationIds()); // ['hero']
console.log(loaded.imageFilenames()); // ['logo.png']
const imgData = loaded.getImage("logo.png"); // BufferNext up: DotLottieBuilder Class