Skip to main content

mux_api 0.1.0 copy "mux_api: ^0.1.0" to clipboard
mux_api: ^0.1.0 copied to clipboard

Pure Dart client for the Mux API: video, data and system resources, JWT playback-token signing, and webhook verification. Runs on Dart servers, CLIs and Flutter apps.

example/mux_api_example.dart

import 'dart:io';

import 'package:mux_api/mux_api.dart';

/// Creates a direct upload, polls it, and signs a playback URL for the asset
/// it produced — the flow a backend runs behind a "upload a video" button.
///
/// Reads MUX_TOKEN_ID, MUX_TOKEN_SECRET, MUX_SIGNING_KEY_ID and
/// MUX_SIGNING_PRIVATE_KEY (PEM or base64 PEM) from the environment.
Future<void> main() async {
  final env = Platform.environment;
  final tokenId = env['MUX_TOKEN_ID'];
  final tokenSecret = env['MUX_TOKEN_SECRET'];
  if (tokenId == null || tokenSecret == null) {
    stderr.writeln('Set MUX_TOKEN_ID and MUX_TOKEN_SECRET to run the example.');
    exitCode = 64;
    return;
  }

  final mux = MuxClient(
    tokenId: tokenId,
    tokenSecret: tokenSecret,
    jwtSigningKeyId: env['MUX_SIGNING_KEY_ID'],
    jwtPrivateKey: env['MUX_SIGNING_PRIVATE_KEY'],
  );

  try {
    final upload = await mux.video.uploads.create(
      const UploadCreateParams(
        corsOrigin: '*',
        newAssetSettings: AssetOptions(
          playbackPolicies: [PlaybackPolicy.signed],
          videoQuality: AssetVideoQuality.plus,
        ),
        timeout: 3600,
      ),
    );
    stdout.writeln('Upload ${upload.id}: PUT your file to ${upload.url}');

    final current = await mux.video.uploads.retrieve(upload.id);
    stdout.writeln('Status: ${current.status?.value}');

    if (current.status == UploadStatus.assetCreated) {
      final asset = await mux.video.assets.retrieve(current.assetId!);
      final signed = asset.playbackIds.firstWhere(
        (playback) => playback.policy == PlaybackPolicy.signed,
      );
      final token = mux.jwt.signPlaybackId(signed.id, expiration: '24h');
      stdout.writeln(
        'Play: https://stream.mux.com/${signed.id}.m3u8?token=$token',
      );
    } else {
      await mux.video.uploads.cancel(upload.id);
      stdout.writeln('Cancelled the unused upload.');
    }
  } on MuxApiException catch (error) {
    stderr.writeln('Mux answered ${error.statusCode}: ${error.messages}');
    exitCode = 1;
  } finally {
    mux.close();
  }
}
0
likes
160
points
487
downloads

Documentation

API reference

Publisher

verified publisherfullphysio.com

Weekly Downloads

Pure Dart client for the Mux API: video, data and system resources, JWT playback-token signing, and webhook verification. Runs on Dart servers, CLIs and Flutter apps.

Repository (GitHub)
View/report issues
Contributing

Topics

#mux #video #api #client

License

MIT (license)

Dependencies

crypto, dart_jsonwebtoken, http, http_parser, meta

More

Packages that depend on mux_api