Asset

View as Markdown

Asset

Assets refer to all the media files (images, videos, PDFs, audio files, and so on) uploaded in your Contentstack repository for future use. These files can be attached and used in multiple entries.

update

The Update Asset call lets you update the name and description of an existing Asset.

import * as contentstack from '@contentstack/management'

const client = contentstack.client({ authtoken })


client.stack({ api_key: 'api_key'}).asset('uid')

.fetch()

.then((asset) => {

 asset.title = 'My New asset'

 asset.description = 'Asset description'

 return asset.update()

})

.then((asset) => console.log(asset))

delete

The Delete asset call will delete an existing asset from the stack.

import * as contentstack from '@contentstack/management'

const client = contentstack.client({ authtoken })


client.stack({ api_key: 'api_key'}).asset('uid')

.delete()

.then((response) => console.log(response.notice))

fetch

The fetch an asset call returns comprehensive information about a specific version of an asset of a stack.
NameTypeDescription
params.versionnumber

Enter the version number of the asset that you want to retrieve. However, to retrieve a specific version of an entry, you need to keep the environment parameter blank.

params.include_publish_detailsstring

Enter 'true' to include the publish details of the entry.

include_asset_scan_statusBoolean

When true, includes the _asset_scan_status field in the asset response (pending, clean, quarantined, or not_scanned). Opt-in; omitted from the request by default.

import * as contentstack from '@contentstack/management'

const client = contentstack.client({ authtoken })


client.stack({ api_key: 'api_key'}).asset('uid')

.fetch()

.then((asset) => console.log(asset))

replace

The Replace asset call will replace an existing asset with another file on the stack.

NameTypeDescription
params.asset (required)object

Asset details to replace.

import * as contentstack from '@contentstack/management'

const client = contentstack.client({ authtoken })


const asset = {

 upload: 'path/to/file.png',

 title: 'Title',

 description: 'Desc'

}


client.stack({ api_key: 'api_key'}).asset('uid').replace(asset)

.then((asset) => console.log(asset))

publish

The publish call is used to publish a specific version of an asset on the desired environment either immediately or at a later date/time.

NameTypeDescription
params.publishDetails (required)object

Details of asset to be publish.

params.localestring

Enter the code of the locale that the entry belongs to.

params.versionnumber

Asset version to be publish

params.scheduledAtstring

Schedule date for publishing Asset.

import * as contentstack from '@contentstack/management'

const client = contentstack.client({ authtoken })


const asset = {

 "locales": [

             "en-us"

             ],

  "environments": [

               "development"

              ]

}

client.stack({ api_key: 'api_key'}).asset('uid')

.publish({ publishDetails: asset, locale: "en-us", version: 1, scheduledAt: "2019-02-08T18:30:00.000Z"})

.then((response) => console.log(response.notice))

unpublish

The unpublish call is used to unpublish a specific version of an asset on the desired environment either immediately or at a later date/time.

NameTypeDescription
params.publishDetails (required)object

Details of asset to be unpublished.

params.localestring

Enter the code of the locale that the entry belongs to.

params.versionnumber

Asset version to be publish

params.scheduledAtstring

Schedule date for publishing Asset.

import * as contentstack from '@contentstack/management'

const client = contentstack.client({ authtoken })


const asset = {

 "locales": [

             "en-us"

             ],

  "environments": [

               "development"

              ]

}

client.stack({ api_key: 'api_key'}).asset('uid')

.unpublish({ publishDetails: asset, locale: "en-us", version: 1, scheduledAt: "2019-02-08T18:30:00.000Z"})

.then((response) => console.log(response.notice))

folder

The Folder allows to fetch and create folders in assets.

NameTypeDescription
uidstring

Folder uid to perform operation

import * as contentstack from '@contentstack/management'

const client = contentstack.client({ authtoken })

client.stack({ api_key: 'api_key'}).asset().folder() 

OR

client.stack({ api_key: 'api_key'}).asset().folder('folder_uid')

create

The Create an asset call creates a new asset.

NameTypeDescription
params.asset (required)object

Asset details to create.

import * as contentstack from '@contentstack/management'

const client = contentstack.client({ authtoken })


const asset = {

 upload: 'path/to/file.png',

 title: 'Title',

 description: 'Desc'

}


client.stack({ api_key: 'api_key'}).asset().create(asset)

.then((asset) => console.log(asset))

query

The Query on Asset will allow to fetch details of all or specific Asset.

NameTypeDescription
params.include_publish_detailsboolean

Enter 'true' to include the publish details of the entry.

params.queryobject

Queries that you can use to fetch filtered results.

include_asset_scan_statusBoolean

When true, includes the _asset_scan_status field in the asset response (pending, clean, quarantined, or not_scanned). Opt-in; omitted from the request by default.

import * as contentstack from '@contentstack/management'

const client = contentstack.client({ authtoken })


client.stack({ api_key: 'api_key'}).asset()

.query({ query: { filename: 'Asset Name' } })

.find()

.then((asset) => console.log(asset))

download

The Download function will get downloadable file in specified format.

NameTypeDescription
param.urlstring

The url for the asset to download

param.responseTypestring

Optional parameter to specify the response type.

import * as contentstack from '@contentstack/management'

const client = contentstack.client({ authtoken })

client.stack({ api_key: 'api_key'}).asset('uid')

.fetch()

.then((asset) => asset.download({responseType: 'blob'}))

.then((response) => // Write response data to destination file. )

import * as contentstack from '@contentstack/management'

const client = contentstack.client({ authtoken })


client.stack({ api_key: 'api_key'}).asset()

.download({url: 'asset_url_to_download', responseType: 'blob'})

.then((response) => // Write response data to destination file. )