Skip to content
Watch a live product demo 👀 See how Sanity powers richer commerce experiences
Sanity
  • Platform

    Sanity Studio

    Flexible editing environment

    APIs

    Connect to anything

    Content Lake

    Real-time database

    Watch product demo

    Features

    Real-time collaboration

    Fearlessly work with content

    Precise content querying

    Treat content as data with GROQ

    Localization

    Coherent messaging across territories

  • Use cases

    E-commerce

    Richer shopping experiences

    Marketing sites

    Control your story

    Products & services

    Innovate and automate

    Mobile apps

    Content backend for every OS

    View all

    Integrations

    Shopify
    Mux
    Vercel
    Netlify
    Algolia
    Cloudinary
    BigCommerce
    Commerce Layer
    Smartling
    Transifex
    View all
  • Learn

    Documentation
    API reference
    Guides
    GROQ cheat sheet
    Sanity UI
    Get started

    Build and share

    Templates
    Tools and plugins
    Schemas and snippets
    Project showcase
    Share your work
    Browse Exchange

    Frameworks

    React
    Vue
    Next.js
    Nuxt.js
    Svelte
    Remix
    Gatsby
    Astro
    Angular
    Eleventy
    View all
  • Discover

    Blog
    Resource library
    Agency partners
    Become a partner
    Technical support
    Talk to sales

    Case studies

    Puma

    Source of truth for all global markets

    Aether

    Unique digital shopping experience

    Morning Brew

    Omnichannel media distribution

    InVision

    Delivering exceptional customer experiences

    View all

    Popular guides

    Structured content
    Content modeling
    Headless CMS
    Headless SEO
    Static websites
    View all
  • Enterprise
  • Pricing
  • Log in
  • Contact sales
  • Get started
Contact salesGet started
Published May 2nd 2020

Say hi to our new asset utilities library!

The new library of asset utilities makes it even more convenient to build with images and files from Sanity.io

Knut Melvær

Principal Developer Marketing Manager

Espen Hovlandsdal

Principal Software Engineer at Sanity

Sanity.io comes with a capable asset pipeline CDN. Upload a large resolution image, and request the size, crop, format, and quality you need. It will be delivered from the edge network, ensuring performance and efficiency for your application. Now we have published a library with a bunch of utility functions that is useful when building with assets from Sanity.

It‘s perhaps not breaking news as far as product releases go. But we think the asset-utils provides some developer experience convenience and wanted to let you know about it. It’s also an example of our continued work to provide those who work with TypeScript, with the types and interfaces they need in their projects.

You add the asset utilities to your project using npm or yarn:

npm i @sanity/asset-utils
# or
yarn add @sanity/asset-utils

Since we wrote asset-utils in TypeScript with TSDoc-annotations, we could generate some comprehensive reference documentation for it as well. You can explore it on GitHub, and it should appear as helpful tool-tips and auto-complete suggestions if you are using VS Code and other capable code editors.

Auto-complete and inline documentation in VS Code

With the asset-utils library, you will be able to generate file URLs from its asset id and the project configuration that you have in your project. It‘s focused around taking the least information possible, and make the most useful thing out of it:

Build a file object from the asset id

import { getFile } from "@sanity/asset-utils";
import { client } from "./client";

const doc = {
  _createdAt: "2019-03-07T08:07:57Z",
  _id: "mFXWUiXzYAP1MwiPjbWVJu",
  _rev: "CbupMhrv0BU2npu4xq78VL",
  _type: "podcast",
  _updatedAt: "2019-08-23T07:40:10Z",
  description: "The story behind GROQ",
  fileAsset: {
    _type: "file",
    asset: {
      _ref: "file-d19e06dcf4aff5a3eafd9d7b58c8703ea6e6b433-m4a",
      _type: "reference"
    }
  },
  title: "How GROQ became GROQ"
};
const file = getFile(doc.fileAsset, client.config());
console.log(file)

Outputs a file object with useful information

{
  "asset": {
    "_id": "file-d19e06dcf4aff5a3eafd9d7b58c8703ea6e6b433-m4a",
    "_type": "sanity.fileAsset",
    "assetId": "d19e06dcf4aff5a3eafd9d7b58c8703ea6e6b433",
    "extension": "m4a",
    "metadata": {},
    "url": "https://cdn.sanity.io/files/zt0rapzo/production/d19e06dcf4aff5a3eafd9d7b58c8703ea6e6b433.m4a",
    "path": "files/zt0rapzo/production/d19e06dcf4aff5a3eafd9d7b58c8703ea6e6b433.m4a"
  }
}

If you want to learn more about Sanity’s asset pipeline, you can go to the post we published in CSS-tricks a while back, and the documentation pages.


We hope you find it helpful, and if it has been, do tell us all about it over at the #i-made-this channel in our community slack.

Product

Sanity StudioAPIsContent LakeSecurity & Compliance
  • Sanity vs Contentful
  • Sanity vs Strapi
  • Sanity vs Wordpress
  • Sanity vs Adobe Experience Manager
  • Sanity vs Hygraph
  • Sanity vs Sitecore
  • Sanity vs Storyblok
  • Sanity vs Contentstack
  • Sanity vs Prismic
  • Sanity vs Drupal
  • Sanity vs ButterCMS

Resources

DocumentationBlogResource libraryCase Studies
  • React Blog
  • Gatsby Blog
  • Next.js Landing Pages
  • Progressive Web Application
  • Single Page Application
  • Svelte & Typescript App
  • Vue & Tailwind Blog
  • Developer Portfolio Templates
  • Form validation with Yup
  • Live Preview with Next.js and Sanity.io
  • Next.js blog
  • Next.js personal website
  • Clean Next.js + Sanity app
  • Clean Remix + Sanity app
  • Clean SvelteKit + Sanity app
  • All Templates
  • Agency partners
  • Technology partners
  • Headless CMS 101
  • What is an API CMS
  • Static Sites 101
  • Headless Commerce 101
  • Headless SEO
  • Localization
  • GraphQL vs REST
  • Content as a Service
  • What is a DXP?
  • Typescript 101
  • Ecommerce SEO
  • React CMS
  • Next.JS CMS
  • CMS for Shopify
  • Content platform
  • Multilingual CMS
  • Static Site CMS
  • Gatsby CMS
  • Node CMS
  • E-commerce CMS
  • Vue CMS
  • Angular CMS
  • GraphQL CMS
  • Newspaper CMS
  • Magazine CMS
  • CMS for apps
  • Remix CMS

Company

Contact SalesEnterpriseCareersTerms of ServicePrivacy PolicyAccessibility Statement

Stay connected

  • GitHub
  • Slack
  • Twitter
  • YouTube
  • Stack Overflow
  • Blog RSS
  • Newsletter
©Sanity 2023