@backblaze-labs/b2-sdk - v0.1.0
    Preparing search index...

    Class B2Client

    High-level B2 client providing ergonomic access to buckets, files, and keys.

    const client = new B2Client({
    applicationKeyId: process.env.B2_APPLICATION_KEY_ID,
    applicationKey: process.env.B2_APPLICATION_KEY,
    })
    await client.authorize()
    const buckets = await client.listBuckets()
    Index

    Constructors

    Properties

    accountInfo: AccountInfo

    Authorization state storage (tokens, URLs, capabilities).

    Low-level client for direct B2 API calls.

    urlGuard: UrlGuard | null

    SSRF allow-list applied by the default FetchTransport. null when a custom transport was supplied — in that case the SDK does not own the guard. Locked down by B2Client.authorize.

    Methods

    • Creates a new B2 bucket.

      Parameters

      • options: {
            bucketInfo?: Record<string, string>;
            bucketName: string;
            bucketType: BucketType;
            corsRules?: CorsRule[];
            defaultRetention?: BucketRetentionPolicy;
            defaultServerSideEncryption?: EncryptionSetting;
            fileLockEnabled?: boolean;
            lifecycleRules?: LifecycleRule[];
            replicationConfiguration?: ReplicationConfiguration;
        }

        Bucket configuration including name, type, and optional settings.

        • OptionalbucketInfo?: Record<string, string>

          Custom key-value metadata stored with the bucket.

        • bucketName: string

          Globally unique bucket name (6-50 chars, letters, digits, hyphens).

        • bucketType: BucketType

          Access level: "allPrivate" or "allPublic".

        • OptionalcorsRules?: CorsRule[]

          CORS rules for browser-based access.

        • OptionaldefaultRetention?: BucketRetentionPolicy

          Default retention policy for new files (requires file lock).

        • OptionaldefaultServerSideEncryption?: EncryptionSetting

          Default server-side encryption for new files.

        • OptionalfileLockEnabled?: boolean

          Enable file lock (Object Lock) on the bucket. Cannot be disabled once set.

        • OptionallifecycleRules?: LifecycleRule[]

          Lifecycle rules for automatic file deletion or hiding.

        • OptionalreplicationConfiguration?: ReplicationConfiguration

          Cross-region replication configuration.

      Returns Promise<Bucket>

      A Bucket handle for the newly created bucket.

    • Creates a new application key with the specified capabilities.

      Parameters

      • options: {
            bucketId?: BucketId;
            capabilities: Capability[];
            keyName: string;
            namePrefix?: string;
            validDurationInSeconds?: number;
        }

        Key configuration including capabilities, name, and optional restrictions.

        • OptionalbucketId?: BucketId

          Restrict the key to a single bucket.

        • capabilities: Capability[]

          Capabilities to grant (e.g., ["readFiles", "writeFiles"]).

        • keyName: string

          Human-readable name for the key.

        • OptionalnamePrefix?: string

          Restrict the key to files with this name prefix.

        • OptionalvalidDurationInSeconds?: number

          Key expiration in seconds from now. Omit for non-expiring keys.

      Returns Promise<FullApplicationKey>

      The full key including the secret (only returned at creation time).

    • Permanently deletes a bucket. The bucket must be empty.

      Parameters

      • id: BucketId

        The unique identifier of the bucket to delete.

      Returns Promise<BucketInfo>

      The deleted bucket metadata.

    • Looks up a single bucket by name.

      Parameters

      • bucketName: string

        The name of the bucket to find.

      Returns Promise<Bucket | null>

      The Bucket handle, or null if not found.

    • Checks whether the authorized application key carries every capability in needed. Returns the missing capabilities so callers can fail fast with a clear error instead of a generic 401/403 from the server.

      Parameters

      • needed: readonly Capability[]

        The capabilities required by the planned operation.

      Returns CapabilityCheckResult

      An object with ok: true when every needed capability is present, otherwise { ok: false, missing: [...] }.

      If authorize has not been called yet.

    • Lists buckets in the account, optionally filtered by ID, name, or type.

      Parameters

      • Optionaloptions: { bucketId?: BucketId; bucketName?: string; bucketTypes?: BucketType[] }

        Optional filters for bucket ID, name, or type.

        • OptionalbucketId?: BucketId

          Filter to a specific bucket by ID.

        • OptionalbucketName?: string

          Filter to a specific bucket by name.

        • OptionalbucketTypes?: BucketType[]

          Filter by bucket types (e.g., ["allPrivate"]).

      Returns Promise<Bucket[]>

      An array of Bucket handles.

    • Lists application keys in the account.

      Parameters

      • Optionaloptions: { pageSize?: number; startApplicationKeyId?: ApplicationKeyId }

        Optional pagination settings.

        • OptionalpageSize?: number

          Maximum number of keys to return per request. Forwarded to the raw API's maxKeyCount parameter.

        • OptionalstartApplicationKeyId?: ApplicationKeyId

          Start listing after this key ID (for pagination).

      Returns Promise<ListKeysResponse>

      A page of application keys with an optional continuation token.

    • Async iterator that yields every application key on the account, automatically handling pagination via listKeys.

      Parameters

      • Optionaloptions: PaginatorOptions

        Pagination + abort options. pageSize is forwarded to maxKeyCount; the default is 1000.

      Returns AsyncIterableIterator<ApplicationKey>

      An async iterable of ApplicationKey entries.

      for await (const key of client.paginateKeys()) {
      console.log(key.keyName, key.capabilities)
      }