useCreateAsset
React Hook for creating a new asset.
useCreateAsset is a React Hook for creating a new asset. It returns a mutate
function that can be called to create an asset. Multiple assets can be created
at once by passing in an array of sources
.
Usage
The asset ID should only be passed to users who are allowed to modify the
asset. Ensure viewers are only provided with the playbackId
, which is a
limited identifier that provides only the ability to stream media using the
Player
.
The following example shows how an asset can be created with the mutate
function. The variables are passed into the useCreateAsset
hook, and
createAsset
return type is defined when the variables are truthy.
Mutation
Return Value
The return value is partially based on Tanstack Query, with some return types aggregated for simplicity.
The value progress
is an array of progress values which contains the upload
progress from tus, as well as the
processing progress of the asset from the provider. These values are polled
consistently until the asset succeeds or fails to be processed by the provider.
Configuration
sources
The sources
used for the asset creation can be an array of either a URL or a
local File/ReadStream/object with a uri
string. It is highly recommended to
use
const assertions
to infer types properly from the sources
parameter - please see below for an
example.
The useCreateAsset
hook uses tus for
resumable uploads and checks for existing uploads by default, before starting
a new upload. It also automatically polls the livepeer provider’s API to
update status
until the import job is complete.
The sources
type can be either a URL or a File/ReadStream:
We are planning on adding further features to the creator ID such as provable claims. Reach out to us on Discord if you’re interested!
If CreateAssetSourceFile
is passed in the array, the useCreateAsset
hook
uses tus for resumable uploads and
checks for existing uploads by default, before starting a new upload.
If CreateAssetSourceUrl
is passed in the array, the hook will make a request
to the background to start an import. This can also be an IPFS URL
(ipfs://<CID>
) or Arweave URL (ar://<HASH>
). Either a URL or file must be
provided.
storage
The storage
configs can be applied on asset creation. See useUpdateAsset
for
more details on these configs. If the storage
parameter is omitted, the asset
will not be stored in decentralized storage.
If you would like to use Arweave as storage, we recommend you upload to Arweave first and then import the video.
The metadata can be overridden when the Asset and its metadata are exported to IPFS - we provide some helper types for metadata best practices based on ERC-721 and ERC-1155:
noWait
The noWait
config option skips the polling mechanism which waits for the Asset
upload to succeed or fail. It will immediately return after the file upload is
complete, or in the case of a URL upload, it will return after the URL is
POST
ed to the backend.
mutationConfig
The mutationConfig
parameter allows for any
Tanstack Query
useMutation
options, such as cacheTime
or retry
. These override any
configs passed by default by the internal hook.