How to Get Started with Sanity and Angular

9 replies
Last updated: Apr 12, 2021
Hello Sanity peeps! I see lots of starter projects for Sanity in use with NextJS, Vue, React, etc but not angular . Is this because it’s not very compatible, or because it’s trivial to connect angular to the Sanity API with a simple HTTPClient and send it a query string and therefore not worth an entire tutorial? I’d love to see a Stackblitz of Sanity with angular !
AI Update

You're spot on with your intuition! It's definitely the latter – connecting Angular to Sanity is straightforward enough that it hasn't generated as many dedicated starter templates as frameworks like Next.js or React. But that doesn't mean it's not well-supported!

Angular works perfectly well with Sanity because Sanity is a headless CMS with a flexible API-first architecture. You can absolutely use Angular's HttpClient to query Sanity's HTTP API directly with GROQ query strings, or you can use the @sanity/client JavaScript library, which works great in Angular projects since it's framework-agnostic.

Here's the basic approach:

Option 1: Using @sanity/client (recommended)

Install the client library (npm install @sanity/client) and configure it in your Angular service:

import { createClient } from '@sanity/client'

export class SanityService {
  private client = createClient({
    projectId: 'your-project-id',
    dataset: 'production',
    useCdn: true,
    apiVersion: '2025-01-01'
  })

  async getPosts() {
    return this.client.fetch(`*[_type == "post"]`)
  }
}

The client.fetch method accepts GROQ query strings and returns promises, making it easy to integrate with Angular's async patterns. You can also pass parameters to your queries for dynamic filtering.

Option 2: Direct HTTP calls

You can also use Angular's HttpClient to hit Sanity's API endpoints directly if you prefer a lighter approach without additional dependencies.

Sanity actually does have Angular resources! There's a guide on creating a single-page application with Angular and Sanity that walks through the integration. The reason you see fewer Angular examples compared to React/Next.js is mainly due to market trends – those frameworks have larger communities in the JAMstack/headless CMS space, so more community examples emerge naturally.

As for a StackBlitz example – while there isn't an official one prominently featured, the integration is straightforward enough that you could spin one up pretty quickly using the guide above! The Angular + Sanity combo works great for content-driven SPAs, blogs, and e-commerce sites. Since Sanity is just a content API, any JavaScript framework can consume it with minimal setup.

Hi! Yes, it's like you said.But for handling portable text, it's nice to have already made component for your library. I'm sure there's one for
angular .I also remember seeing this course in the twitter feed not long time ago:
<https://explorers.netlify.com/learn/
angular -dynamic-data-with-sanity-io>
Great! I’ll check it out. Yeah the articles and tutorials I did find are for angular but also include other technologies I don’t necessarily want to add right now like Netflify hooks, angular Univeral server-side rendering, etc etc. I’m excited to connect to Sanity and get rolling! 🙂. But this will get me aware of those technologies and add them in the future when I understand the use case for them. Thank you for the link!
ah yes, sure, you don't need all of those, especially if you're building a SPA.I recommend using this client:
https://github.com/rexxars/picosanity
for rendering images, I use, like almost everybody here I guess, this library: https://www.sanity.io/docs/image-url Besides all the images manipulation capabilities, the main advantage for me is that I can just pass it an image asset and it will automatically resolve the URL of the image (otherwise you have to resolve the reference yourself inside the query).
and I highly recommend you make query using GROQ. I love that query language!
Yeah my brain really took to the GROQ syntax when I saw a video about it. And that image-url builder looks sweet. Thanks! It’s going to be a fun Sunday checking all this out!
Hey
user E
, why is Sanity always paired with Netlify functions and never just used as an endpoint with query strings at the end for queries? What am I missing?
It's strange that in this tutorial data are fetched through a Netlify Function. I guess Netlify just wanted to showcase this feature.I would only use serverless function if I'm working with a private dataset.

Also, you're building a SPA (no server rendering at build time), you also don't need to use Netlify build hooks.
Ok thank you for confirming. I thought it was to build complex queries that contain characters that would not be accepted as a query string like
?query=*[2]
but I could always convert those to html encoding like
%20
and all that

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?