# Query Examples with the Content Delivery API

### About this export

| Field | Value |
| --- | --- |
| **content_type** | lesson |
| **platform** | contentstack-academy |
| **source_url** | https://www.contentstack.com/academy/courses/contentstack-for-developers/query-examples-with-the-content-delivery-api |
| **course_slug** | contentstack-for-developers |
| **lesson_slug** | query-examples-with-the-content-delivery-api |
| **markdown_file_url** | /academy/md/courses/contentstack-for-developers/query-examples-with-the-content-delivery-api.md |
| **generated_at** | 2026-05-04T05:36:51.209Z |

> Part of **[Contentstack for Developers](https://www.contentstack.com/academy/courses/contentstack-for-developers)** on Contentstack Academy. **Academy MD v3** — structured for retrieval; no quiz or assessment keys.

<!-- ai_metadata: {"lesson_id":"14","type":"text","duration_minutes":1,"topics":["Query","Examples","with","the","Content","Delivery"]} -->

#### Lesson text

# Query Examples with the Content Delivery API

## **Query by URL**

const getContentByURL = async ({ contentType, slug, refUids, locale, filter }) => {  
  try {  
    const Query = global.Stack  
      .ContentType(contentType)  
      .Query()  
      .language(locale)  
    if (refUids) {  
      let refs = refUids.replace(/ /g, '').split(',')  
      Query.includeReference(refs)  
    }  
    let result = await Query  
      .includeFallback()  
      .where('url', \`/${slug}\`)  
      .toJSON()  
      .includeEmbeddedItems()  
      .addParam('include\_metadata', 'true')  
      .find()  
    if (result && result.length > 0 && result\[0\].length <= 0) { // for trailing slash match  
      result = await Query  
        .includeFallback()  
        .where('url', \`/${slug}/\`)  
        .toJSON()  
        .includeEmbeddedItems()  
        .addParam('include\_metadata', 'true')  
        .find()  
    }  
  
    (global.env === 'preview' || global.env === 'staging-preview' ||global.env === 'development') ? Contentstack.Utils.addEditableTags(result\[0\]\[0\], contentType, true, locale) : ''  
    //metadata key required for \_metadata in cms entry data  
    if(result.length > 0 && result\[0\].length > 0) {  
      result\[0\]\[0\] = {...result\[0\]\[0\], metadata: result\[0\]\[0\]}  
    }  
    if (result) return result\[0\]\[0\]  
  }  
  catch (error) {  
    throw new Error(error)  
  }  
}

  

## **Query by UID**

const getContentByUID= async ({ contentType, entryUid, refUids, locale }) => {  
  try {  
    const Query = global.Stack  
      .ContentType(contentType)  
      .Entry(entryUid)  
      .language(locale)  
      .includeFallback()  
      .includeEmbeddedItems()  
    if (refUids) {  
      let refs = refUids.replace(/ /g, '').split(',')  
      Query.includeReference(refs)  
    }  
    let result = await Query.toJSON().fetch()  
  
    if (result) return result  
  }  catch (error) {  
    return error  
  }  
}

  

## **Example to Get All Entries**

const getEntries = async ({ contentType, refUids, locale }) => {  
  let collectionData  
  try {  
    const Query = global.Stack.ContentType(contentType)  
      .Query()  
      .language(locale)  
    if (refUids) {  
      let refs = refUids.replace(/ /g, '').split(',')  
      Query.includeReference(refs)  
    }  
    let result = await Query  
      .includeFallback()  
      .toJSON()  
      .includeEmbeddedItems()  
      .addParam('include\_metadata', 'true')  
      .find()        
  
    if (result && result.length > 0 && result\[0\].length <= 1) { //for entry type = single  
      (global.env === 'preview' || global.env === 'staging-preview' || global.env === 'development') ? Contentstack.Utils.addEditableTags(result\[0\]\[0\], contentType, true, locale) : ''  
      //metadata key required for \_metadata in cms entry data  
      result\[0\]\[0\] = {...result\[0\]\[0\], metadata: result\[0\]\[0\]}  
      return result\[0\]\[0\]  
    }  
      
    //for entry type = multiple  
    (global.env === 'preview' || global.env === 'staging-preview' || global.env === 'development') ? Contentstack.Utils.addEditableTags(result\[0\]\[0\], contentType, true, locale) : ''  
    if (result) return result  
  }  
  catch (error) {  
    res.status(500).send(\`Internal Server Error\\n: ${error}\`)  
    // throw new Error(error)  
  }  
}

#### Key takeaways

- Connect **Query Examples with the Content Delivery API** back to your stack configuration before moving to the next module.
- Capture one concrete artifact (screenshot, Postman call, or code snippet) that proves the step works in your environment.
- Re-read the delivery versus management boundary for anything you changed in the entry model.

## Supplement for indexing

### Content summary

Query Examples with the Content Delivery API. Query Examples with the Content Delivery API Query by URL const getContentByURL = async ({ contentType, slug, refUids, locale, filter }) = { try { const Query = global.Stack .ContentType(contentType) .Query() .language(locale) if (refUids) { let refs = refUids.replace(/ /g, '').split(',') Query.includeReference(refs) } let result = await Query .includeFallback() .where('url', \ /${slug}\ ) .toJSON() .includeEmbeddedItems() .addParam('include\ metadata', 'true') .find() if (result && result.length 0 && result\ 0\ .length <= 0) { // for trailing slash match result = await Query .includeFallback() .where('url', \ /${slug}/\ ) .toJSON() .includeEmbeddedItems() .addParam('include\ metadata', 'tru

### Retrieval tags

- Query
- Examples
- with
- the
- Content
- Delivery
- contentstack-for-developers
- lesson 14
- Query Examples with the Content Delivery API
- contentstack-for-developers lesson

### Indexing notes

Index this lesson as a primary chunk tagged with lesson_id "14" and topics: [Query, Examples, with, the, Content, Delivery].
Parent course slug: contentstack-for-developers. Use asset_references URLs as thumbnail hints in search results when present.
Never surface LMS quiz content or assessment answers from this file.

### Asset references

_No image or video thumbnail URLs were extracted._

### External links

| Label | URL |
| --- | --- |
| Contentstack Academy home | `https://www.contentstack.com/academy/` |
| Training instance setup | `https://www.contentstack.com/academy/training-instance` |
| Academy playground (GitHub) | `https://github.com/contentstack/contentstack-academy-playground` |
| Contentstack documentation | `https://www.contentstack.com/docs/` |
