Skip to content
👀 See Sanity in action: Watch product demo now →
Sanity
Get started
  • Sanity Studio - Flexible editing environment
  • Content Lake - Real-time database
  • Developer experience - Tooling you love
  • Structured content - The philosophy behind Sanity
  • Review changes - View edits & rollback instantly
  • Image pipeline - On-demand transformations
  • E-commerce - Better shopping experiences
  • Marketing sites - Control your story
  • Products & services - Innovate and automate
  • Mobile apps - Content backend for every OS
  • Aether - Unique digital shopping experience
  • Morning Brew - Omnichannel media distribution
  • InVision - Delivering exceptional customer experiences
  • DataStax - Personalization for global audience
  • React
  • Gatsby
  • Next
  • Nuxt
  • Eleventy
  • Netlify
  • Vercel
  • Algolia
  • Documentation
  • Reference
  • Guides
  • Resource library
  • Headless CMS
  • Tools & plugins
  • Project showcase
  • Schemas & snippets
  • Agency partners
  • Technology partners
  • Get support
  • Share your work
  • Enterprise SSO
EnterprisePricing
Contact salesLog inGet 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

Head of Developer Relations at Sanity.io

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.

Platform

Structured ContentDeveloper experienceContent LakeSanity StudioSecurity & Compliance
  • Sanity vs Contentful
  • Sanity vs Strapi
  • Sanity vs Adobe Experience Manager
  • Sanity vs Hygraph
  • Sanity vs Sitecore
  • Sanity vs Storyblok
  • Sanity vs Contentstack
  • Sanity vs Prismic
  • Sanity vs Drupal

Resources

Documentation
  • 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
Resource library
  • Agency partners
  • Technology partners
  • Blog Template
  • Personal Website Template
  • Developer Portfolio Templates
  • All Templates
Case Studies
  • Headless CMS
  • What is an API CMS
  • Static Sites 101
  • Headless SEO
  • Localization
  • GraphQL vs REST
  • What is a DXP?
  • Typescript 101
  • 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
  • Mobile apps CMS

Company

Contact SalesEnterpriseCareersTerms of ServiceAccessibility Statement

Stay connected

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