ContentType Collection

View as Markdown

ContentType Collection

ContentTypeQuery lists the content types of a stack, with the schema of each. It returns the structure of your content, not the entries. Use this class to

  • list every content type in the stack
  • include the schemas of the global fields that the content types use
  • sort the list and page through it

Get a ContentTypeQuery with stack.contentType() and no UID.

addParams

addParams merges several query parameters into the request that find sends.

NameTypeDescription
paramObj (required){ [key: string]: string | boolean | number }

Query parameters to merge into the query.

Default: Not applicable

Returns: ContentTypeQuery. The same query, for chaining.

Validation

  • There is no client-side validation. addParams merges any key and value, and an empty object changes nothing.
  • Passing a value that is not a string, boolean, or number, such as an array, is a compile error.
  • Errors surface on the find() call that follows, not on addParams(). find() throws an APIError when the request fails, carrying the HTTP status, the API error_code, and the error_message.

Additional Resource Refer to Delivery API Errors for the full error code list.

Behavior

  • Client-side only. addParams merges the object into the query parameters, and find sends them.
  • addParams overwrites a key that already exists. Keys you do not pass stay as they are.
  • Unlike the param method, addParams accepts boolean values. The request sends them as true or false.
  • Known issue: In 5.x, includeGlobalFieldSchema does not compile after addParams, because TypeScript types the return value as Query. Call the includeGlobalFieldSchema method before addParams, as Chain order on the ContentType Collection class page describes.

Example

Basic usage: set several parameters at once

import contentstack from '@contentstack/delivery-sdk';

const stack = contentstack.stack({
  apiKey: '<API_KEY>',
  deliveryToken: '<DELIVERY_TOKEN>',
  environment: '<ENVIRONMENT>',
});

try {
  const result = await stack.contentType().addParams({ include_count: true, limit: 5 }).find();
  console.log('returned', result.content_types?.length, 'of', result.count);
} catch (error: any) {
  // Thrown for API rejections and network failures
  console.error('Request failed:', error.status, error.error_code, error.error_message);
}

Edge case: merging keeps the keys you do not pass

import contentstack from '@contentstack/delivery-sdk';

const stack = contentstack.stack({
  apiKey: '<API_KEY>',
  deliveryToken: '<DELIVERY_TOKEN>',
  environment: '<ENVIRONMENT>',
});

try {
  // limit comes from addParams, skip from the earlier call stays.
  const result = await stack.contentType().skip(1).addParams({ limit: 3 }).find();
  console.log('returned', result.content_types?.length);
} catch (error: any) {
  console.error(error);
}

find

find retrieves the content types of the stack, with the parameters the query holds.

NameTypeDescription
encodeboolean

URL-encodes the values of the query parameter.

Default: false

Returns: Promise<FindResponse<T>>. The matching content types.

Validation

  • There is no client-side validation. find sends whatever parameters the query holds.
  • find exists only on ContentTypeQuery. Calling it on stack.contentType('<CONTENT_TYPE_UID>') is a compile error.
  • find throws an APIError when the request fails, carrying the HTTP status, the API error_code, and the error_message. A network failure throws status 0, with the Node.js error code, such as ENOTFOUND, in error_code.

Additional Resource Refer to Delivery API Errors for the full error code list.

Behavior

  • One call sends one GET /content_types request and returns one page. The SDK does not iterate pages.
  • find returns the response body unchanged, and TypeScript types it as FindResponse<T>. The list is under content_types, and count is present when you chained the includeCount method.
  • find<T>() types content_types as unknown[], whatever T you pass, because T types only the entry, asset, and global field arrays of FindResponse. Cast the array to read it, for example result.content_types as BaseContentType[].
  • encode has no effect on this class. It encodes the query filter object, and ContentTypeQuery has no filter methods that fill it.
  • Calling find again on the same object sends the same parameters again.

Example

Basic usage: list the content types of the stack

import contentstack from '@contentstack/delivery-sdk';

const stack = contentstack.stack({
  apiKey: '<API_KEY>',
  deliveryToken: '<DELIVERY_TOKEN>',
  environment: '<ENVIRONMENT>',
});

try {
  const result = await stack.contentType().find();
  console.log(result.content_types?.length, 'content types');
} catch (error: any) {
  // Thrown for API rejections and network failures
  console.error('Request failed:', error.status, error.error_code, error.error_message);
}

All parameters: pass encode and type the list

import contentstack from '@contentstack/delivery-sdk';
import type { BaseContentType } from '@contentstack/delivery-sdk';

const stack = contentstack.stack({
  apiKey: '<API_KEY>',
  deliveryToken: '<DELIVERY_TOKEN>',
  environment: '<ENVIRONMENT>',
});

try {
  // encode has no effect here, because ContentTypeQuery builds no query filter.
  const result = await stack.contentType().includeCount().find(true);
  const contentTypes = (result.content_types ?? []) as BaseContentType[];
  for (const contentType of contentTypes) {
    console.log(contentType.uid, contentType.title, contentType.schema.length, 'fields');
  }
  console.log('total:', result.count);
} catch (error: any) {
  console.error(error);
}

Edge case: filter the list in your own code

import contentstack from '@contentstack/delivery-sdk';
import type { BaseContentType } from '@contentstack/delivery-sdk';

const stack = contentstack.stack({
  apiKey: '<API_KEY>',
  deliveryToken: '<DELIVERY_TOKEN>',
  environment: '<ENVIRONMENT>',
});

try {
  // ContentTypeQuery has no where(), so filter after find returns.
  const result = await stack.contentType().find();
  const contentTypes = (result.content_types ?? []) as BaseContentType[];
  const pages = contentTypes.filter((contentType) => contentType.schema.some((field: any) => field.uid === 'url'));
  console.log('content types with a url field:', pages.map((contentType) => contentType.uid));
} catch (error: any) {
  console.error(error);
}

includeCount

includeCount adds the total number of content types in the stack to the response, as count.

Returns: ContentTypeQuery. The same query, for chaining.

Validation

  • There is no client-side validation. includeCount takes no input.
  • Errors surface on the find() call that follows, not on includeCount(). find() throws an APIError when the request fails, carrying the HTTP status, the API error_code, and the error_message.

Additional Resource Refer to Delivery API Errors for the full error code list.

Behavior

  • Client-side only. includeCount sets the include_count query parameter to 'true', and find sends it.
  • The response then carries count, the total number of content types. It can exceed the number of content types on one page.
  • Calling it twice changes nothing. Call the removeParam method with 'include_count' to drop the flag.
  • Known issue: In 5.x, includeGlobalFieldSchema does not compile after includeCount, because TypeScript types the return value as Query. Call the includeGlobalFieldSchema method before includeCount, as Chain order on the ContentType Collection class page describes.

Example

Basic usage: count the content types in the stack

import contentstack from '@contentstack/delivery-sdk';

const stack = contentstack.stack({
  apiKey: '<API_KEY>',
  deliveryToken: '<DELIVERY_TOKEN>',
  environment: '<ENVIRONMENT>',
});

try {
  const result = await stack.contentType().includeCount().find();
  console.log('total content types:', result.count);
} catch (error: any) {
  // Thrown for API rejections and network failures
  console.error('Request failed:', error.status, error.error_code, error.error_message);
}

Pagination: find out whether more content types remain

import contentstack from '@contentstack/delivery-sdk';

const stack = contentstack.stack({
  apiKey: '<API_KEY>',
  deliveryToken: '<DELIVERY_TOKEN>',
  environment: '<ENVIRONMENT>',
});

try {
  const result = await stack.contentType().limit(2).includeCount().find();
  const returned = result.content_types?.length ?? 0;
  console.log('returned', returned, 'of', result.count, 'more remain:', returned < (result.count ?? 0));
} catch (error: any) {
  console.error(error);
}

includeGlobalFieldSchema

includeGlobalFieldSchema asks the API to include the schema of each global field that a listed content type uses.

Returns: ContentTypeQuery. The same query, for chaining.

Validation

  • There is no client-side validation. includeGlobalFieldSchema takes no input.
  • Known issue: In 5.x, calling it after a method such as limit or includeCount is a compile error, because TypeScript types those methods as returning Query. Call includeGlobalFieldSchema first, as Chain order on the ContentType Collection class page describes.
  • Errors surface on the find() call that follows, not on includeGlobalFieldSchema(). find() throws an APIError when the request fails, carrying the HTTP status, the API error_code, and the error_message.

Additional Resource Refer to Delivery API Errors for the full error code list.

Behavior

  • Client-side only. includeGlobalFieldSchema sets the include_global_field_schema query parameter to 'true', and find sends it.
  • In the response, each global field in a content type schema carries a schema array with the fields of that global field. This includes a global field inside a modular block. Without the flag, a global field carries only its reference_to UID.
  • Calling it twice changes nothing. Call the removeParam method with 'include_global_field_schema' to drop the flag.
  • Returns the same ContentTypeQuery, and TypeScript keeps the ContentTypeQuery type, so every method of the class stays available after it.

Limitations

  • Does not apply to a single content type. stack.contentType('<CONTENT_TYPE_UID>') has no equivalent method, and its fetch sends no extra parameters. Call includeGlobalFieldSchema on the list and pick the content type by uid instead, as the second example shows.

Example

Basic usage: list content types with global field schemas

import contentstack from '@contentstack/delivery-sdk';

const stack = contentstack.stack({
  apiKey: '<API_KEY>',
  deliveryToken: '<DELIVERY_TOKEN>',
  environment: '<ENVIRONMENT>',
});

try {
  const result = await stack.contentType().includeGlobalFieldSchema().find();
  console.log(result.content_types?.length);
} catch (error: any) {
  // Thrown for API rejections and network failures
  console.error('Request failed:', error.status, error.error_code, error.error_message);
}

Single content type: read one schema with global field schemas

import contentstack from '@contentstack/delivery-sdk';
import type { BaseContentType } from '@contentstack/delivery-sdk';

const stack = contentstack.stack({
  apiKey: '<API_KEY>',
  deliveryToken: '<DELIVERY_TOKEN>',
  environment: '<ENVIRONMENT>',
});

try {
  // includeGlobalFieldSchema first, then the inherited limit.
  const result = await stack.contentType().includeGlobalFieldSchema().limit(100).find();
  const contentTypes = (result.content_types ?? []) as BaseContentType[];
  const match = contentTypes.find((contentType) => contentType.uid === '<CONTENT_TYPE_UID>');
  const globalFields = match?.schema.filter((field: any) => field.data_type === 'global_field');
  console.log(match?.title, 'global fields:', globalFields?.map((field: any) => field.uid));
} catch (error: any) {
  console.error(error);
}

limit

limit caps the number of content types the response contains.

NameTypeDescription
key (required)number

Maximum number of content types to return.

Default: Not applicable

Returns: ContentTypeQuery. The same query, for chaining.

Validation

  • There is no client-side validation. limit stores zero, a negative number, or a fraction as given. The API ignores a limit of 0 or less and returns its default page of up to 100 content types.
  • Passing a string, such as limit('5'), is a compile error.
  • Errors surface on the find() call that follows, not on limit(). find() throws an APIError when the request fails, carrying the HTTP status, the API error_code, and the error_message.

Additional Resource Refer to Delivery API Errors for the full error code list.

Behavior

  • Client-side only. limit sets the limit query parameter, and find sends it.
  • Omitting limit does not return every content type. The API returns at most 100 content types per request. See Pagination on the ContentType Collection class page.
  • A second call replaces the first value, because the query holds one limit value.
  • Known issue: In 5.x, includeGlobalFieldSchema does not compile after limit, because TypeScript types the return value as Query. Call the includeGlobalFieldSchema method before limit, as Chain order on the ContentType Collection class page describes.

Example

Basic usage: return at most five content types

import contentstack from '@contentstack/delivery-sdk';

const stack = contentstack.stack({
  apiKey: '<API_KEY>',
  deliveryToken: '<DELIVERY_TOKEN>',
  environment: '<ENVIRONMENT>',
});

try {
  const result = await stack.contentType().limit(5).find();
  console.log('returned', result.content_types?.length);
} catch (error: any) {
  // Thrown for API rejections and network failures
  console.error('Request failed:', error.status, error.error_code, error.error_message);
}

Pagination: read every content type one page at a time

import contentstack from '@contentstack/delivery-sdk';

const stack = contentstack.stack({
  apiKey: '<API_KEY>',
  deliveryToken: '<DELIVERY_TOKEN>',
  environment: '<ENVIRONMENT>',
});

const pageSize = 10;
const all: unknown[] = [];

try {
  let skip = 0;
  let total = 0;
  do {
    // A fresh query per page, so each request carries its own skip value.
    const page = await stack.contentType().skip(skip).limit(pageSize).includeCount().find();
    const items = page.content_types ?? [];
    all.push(...items);
    total = page.count ?? 0;
    skip += pageSize;
    if (items.length === 0) break;
  } while (skip < total);
  console.log('read', all.length, 'of', total, 'content types');
} catch (error: any) {
  console.error(error);
}

orderByAscending

orderByAscending sorts the content types in ascending order of one field.

NameTypeDescription
key (required)string

UID of the field to sort by.

Default: Not applicable

Returns: ContentTypeQuery. The same query, for chaining.

Validation

  • There is no client-side validation. orderByAscending stores an empty string or an unknown field UID as given.
  • Calling orderByAscending without an argument is a compile error.
  • Errors surface on the find() call that follows, not on orderByAscending(). find() throws an APIError when the request fails, carrying the HTTP status, the API error_code, and the error_message.

Additional Resource Refer to Delivery API Errors for the full error code list.

Behavior

  • Client-side only. orderByAscending sets the asc query parameter to the field UID, and find sends it.
  • A second call replaces the field, because the query holds one asc value.
  • orderByAscending and the orderByDescending method write different keys, so calling both sends asc and desc together. Call the removeParam method with 'desc' to keep one sort.
  • Known issue: In 5.x, includeGlobalFieldSchema does not compile after orderByAscending, because TypeScript types the return value as Query. Call the includeGlobalFieldSchema method before orderByAscending, as Chain order on the ContentType Collection class page describes.

Example

Basic usage: sort content types by title

import contentstack from '@contentstack/delivery-sdk';

const stack = contentstack.stack({
  apiKey: '<API_KEY>',
  deliveryToken: '<DELIVERY_TOKEN>',
  environment: '<ENVIRONMENT>',
});

try {
  const result = await stack.contentType().orderByAscending('title').find();
  console.log(result.content_types?.length, 'content types sorted by title');
} catch (error: any) {
  // Thrown for API rejections and network failures
  console.error('Request failed:', error.status, error.error_code, error.error_message);
}

Edge case: switch the sort direction on an existing query

import contentstack from '@contentstack/delivery-sdk';

const stack = contentstack.stack({
  apiKey: '<API_KEY>',
  deliveryToken: '<DELIVERY_TOKEN>',
  environment: '<ENVIRONMENT>',
});

// The query starts with the opposite sort.
const query = stack.contentType().orderByDescending('created_at');

try {
  // Drop the old key, then set the new one, so the request carries one sort.
  const result = await query.removeParam('desc').orderByAscending('title').find();
  console.log(result.content_types?.length);
} catch (error: any) {
  console.error(error);
}

orderByDescending

orderByDescending sorts the content types in descending order of one field.

NameTypeDescription
key (required)string

UID of the field to sort by.

Default: Not applicable

Returns: ContentTypeQuery. The same query, for chaining.

Validation

  • There is no client-side validation. orderByDescending stores an empty string or an unknown field UID as given.
  • Calling orderByDescending without an argument is a compile error.
  • Errors surface on the find() call that follows, not on orderByDescending(). find() throws an APIError when the request fails, carrying the HTTP status, the API error_code, and the error_message.

Additional Resource Refer to Delivery API Errors for the full error code list.

Behavior

  • Client-side only. orderByDescending sets the desc query parameter to the field UID, and find sends it.
  • A second call replaces the field, because the query holds one desc value.
  • orderByDescending and the orderByAscending method write different keys, so calling both sends asc and desc together. Call the removeParam method with 'asc' to keep one sort.
  • Known issue: In 5.x, includeGlobalFieldSchema does not compile after orderByDescending, because TypeScript types the return value as Query. Call the includeGlobalFieldSchema method before orderByDescending, as Chain order on the ContentType Collection class page describes.

Example

Basic usage: sort content types by updated_at

import contentstack from '@contentstack/delivery-sdk';

const stack = contentstack.stack({
  apiKey: '<API_KEY>',
  deliveryToken: '<DELIVERY_TOKEN>',
  environment: '<ENVIRONMENT>',
});

try {
  const result = await stack.contentType().orderByDescending('updated_at').find();
  console.log(result.content_types?.length, 'content types sorted by updated_at');
} catch (error: any) {
  // Thrown for API rejections and network failures
  console.error('Request failed:', error.status, error.error_code, error.error_message);
}

Edge case: switch the sort direction on an existing query

import contentstack from '@contentstack/delivery-sdk';

const stack = contentstack.stack({
  apiKey: '<API_KEY>',
  deliveryToken: '<DELIVERY_TOKEN>',
  environment: '<ENVIRONMENT>',
});

// The query starts with the opposite sort.
const query = stack.contentType().orderByAscending('created_at');

try {
  // Drop the old key, then set the new one, so the request carries one sort.
  const result = await query.removeParam('asc').orderByDescending('updated_at').find();
  console.log(result.content_types?.length);
} catch (error: any) {
  console.error(error);
}

param

param sets one query parameter on the request that find sends.

NameTypeDescription
key (required)string

Name of the query parameter.

Default: Not applicable
value (required)string | number

Value of the query parameter.

Default: Not applicable

Returns: ContentTypeQuery. The same query, for chaining.

Validation

  • There is no client-side validation. param stores any key, including one the API does not recognize, and any value, including an empty string.
  • Passing a boolean value, such as param('include_count', true), is a compile error. Pass the string 'true', or use the addParams method, which accepts booleans.
  • Errors surface on the find() call that follows, not on param(). find() throws an APIError when the request fails, carrying the HTTP status, the API error_code, and the error_message.

Additional Resource Refer to Delivery API Errors for the full error code list.

Behavior

  • Client-side only. param writes the key and value into the query parameters, and find sends them.
  • param replaces a key that another method already set. For example, param('limit', 5) replaces a value from the limit method.
  • Use param for an API parameter that ContentTypeQuery has no method for.
  • Known issue: In 5.x, includeGlobalFieldSchema does not compile after param, because TypeScript types the return value as Query. Call the includeGlobalFieldSchema method before param, as Chain order on the ContentType Collection class page describes.

Example

Basic usage: set a parameter the class has no method for

import contentstack from '@contentstack/delivery-sdk';

const stack = contentstack.stack({
  apiKey: '<API_KEY>',
  deliveryToken: '<DELIVERY_TOKEN>',
  environment: '<ENVIRONMENT>',
});

try {
  const result = await stack.contentType().param('include_branch', 'true').find();
  console.log(result.content_types?.length);
} catch (error: any) {
  // Thrown for API rejections and network failures
  console.error('Request failed:', error.status, error.error_code, error.error_message);
}

Edge case: a later call replaces an earlier value

import contentstack from '@contentstack/delivery-sdk';

const stack = contentstack.stack({
  apiKey: '<API_KEY>',
  deliveryToken: '<DELIVERY_TOKEN>',
  environment: '<ENVIRONMENT>',
});

try {
  // The second value replaces the first, so the request carries limit=2.
  const result = await stack.contentType().limit(10).param('limit', 2).find();
  console.log('returned', result.content_types?.length);
} catch (error: any) {
  console.error(error);
}

removeParam

removeParam deletes one query parameter from the request that find sends.

NameTypeDescription
key (required)string

Name of the query parameter to delete.

Default: Not applicable

Returns: ContentTypeQuery. The same query, for chaining.

Validation

  • There is no client-side validation. A key the query does not hold changes nothing.
  • Calling removeParam without an argument is a compile error.
  • Errors surface on the find() call that follows, not on removeParam(). find() throws an APIError when the request fails, carrying the HTTP status, the API error_code, and the error_message.

Additional Resource Refer to Delivery API Errors for the full error code list.

Behavior

  • Client-side only. removeParam deletes the key from the query parameters, so the next find omits it.
  • It removes keys that any method set, for example include_count from the includeCount method or limit from the limit method.
  • It cannot remove environment or the stack-level locale. The stack sends those on every request, and they never enter the query parameters.
  • Known issue: In 5.x, includeGlobalFieldSchema does not compile after removeParam, because TypeScript types the return value as Query. Call the includeGlobalFieldSchema method before removeParam, as Chain order on the ContentType Collection class page describes.

Example

Basic usage: drop a flag before the next request

import contentstack from '@contentstack/delivery-sdk';

const stack = contentstack.stack({
  apiKey: '<API_KEY>',
  deliveryToken: '<DELIVERY_TOKEN>',
  environment: '<ENVIRONMENT>',
});

const query = stack.contentType().includeCount();

try {
  // The request no longer asks for the total count.
  const result = await query.removeParam('include_count').find();
  console.log('count present:', result.count !== undefined);
} catch (error: any) {
  // Thrown for API rejections and network failures
  console.error('Request failed:', error.status, error.error_code, error.error_message);
}

Edge case: stack-level parameters stay

import contentstack from '@contentstack/delivery-sdk';

const stack = contentstack.stack({
  apiKey: '<API_KEY>',
  deliveryToken: '<DELIVERY_TOKEN>',
  environment: '<ENVIRONMENT>',
});

try {
  // environment comes from the stack, so this call changes nothing.
  const result = await stack.contentType().removeParam('environment').find();
  console.log(result.content_types?.length);
} catch (error: any) {
  console.error(error);
}

skip

skip leaves out a number of content types from the start of the list, for pagination.

NameTypeDescription
key (required)number

Number of content types to leave out.

Default: Not applicable

Returns: ContentTypeQuery. The same query, for chaining.

Validation

  • There is no client-side validation. skip stores a negative number or a fraction as given. The API treats a negative skip as 0.
  • Passing a string, such as skip('10'), is a compile error.
  • Errors surface on the find() call that follows, not on skip(). find() throws an APIError when the request fails, carrying the HTTP status, the API error_code, and the error_message.

Additional Resource Refer to Delivery API Errors for the full error code list.

Behavior

  • Client-side only. skip sets the skip query parameter, and find sends it.
  • Combine skip with the limit method to read one page at a time.
  • A skip value at or past the total returns an empty content_types array, not an error.
  • A second call replaces the first value, because the query holds one skip value.
  • Known issue: In 5.x, includeGlobalFieldSchema does not compile after skip, because TypeScript types the return value as Query. Call the includeGlobalFieldSchema method before skip, as Chain order on the ContentType Collection class page describes.

Example

Basic usage: read the second page of ten content types

import contentstack from '@contentstack/delivery-sdk';

const stack = contentstack.stack({
  apiKey: '<API_KEY>',
  deliveryToken: '<DELIVERY_TOKEN>',
  environment: '<ENVIRONMENT>',
});

try {
  const result = await stack.contentType().orderByAscending('created_at').skip(10).limit(10).find();
  console.log('second page:', result.content_types?.length);
} catch (error: any) {
  // Thrown for API rejections and network failures
  console.error('Request failed:', error.status, error.error_code, error.error_message);
}

Edge case: skip past the last item

import contentstack from '@contentstack/delivery-sdk';

const stack = contentstack.stack({
  apiKey: '<API_KEY>',
  deliveryToken: '<DELIVERY_TOKEN>',
  environment: '<ENVIRONMENT>',
});

try {
  const counted = await stack.contentType().limit(1).includeCount().find();
  const total = counted.count ?? 0;

  // Skipping every content type returns an empty array, not an error.
  const result = await stack.contentType().skip(total).find();
  console.log('past the end:', result.content_types?.length);
} catch (error: any) {
  console.error(error);
}