React Native Delivery SDK
Contentstack is a headless CMS with an API-first approach. It is a CMS that developers can use to build powerful cross-platform applications in their favorite languages. Build your application frontend, and Contentstack will take care of the rest. Read More.
Contentstack provides React Native Delivery SDK to build application on top of React Native. Given below is the detailed guide and helpful resources to get started with our React Native Delivery SDK.
Prerequisite
You need Node.js version 4.4.7 or later installed to use the Contentstack JavaScript Delivery SDK.
Setup and Installation
React Native uses the Javascript SDK to create apps. To use the JavaScript Delivery SDK, install ist via npm:
npm i contentstack
To import the SDK in your project, use the following command:
import Contentstack from 'contentstack/react-native'
To initialize the SDK, you will need to specify the API Key, Delivery Token, and Environment Name of your stack.
const Stack = Contentstack.Stack({ "api_key": "api_key", "delivery_token": "delivery_token", "environment": "environment" });
To initialize the SDK for the Europe, Azure NA, Azure EU, or GCP NA region, refer to the code below:
FOR EU const Stack = Contentstack.Stack({"api_key":"api_key","delivery_token":"delivery_token","environment":"environment","region":Contentstack.Region.EU}); FOR AZURE_EU const Stack = Contentstack.Stack({"api_key":"api_key","delivery_token":"delivery_token","environment":"environment","region":Contentstack.Region.AZURE_EU}); FOR AZURE_NA const Stack = Contentstack.Stack({"api_key":"api_key","delivery_token":"delivery_token","environment": "environment", "region": Contentstack.Region.AZURE_NA}); FOR GCP_NA const Stack = Contentstack.Stack({"api_key":"api_key","delivery_token":"delivery_token","environment":"environment","region":Contentstack.Region.GCP_NA});
Quickstart in 5 mins
Initializing your SDK
You will need to specify the API key, Access token, and Environment Name of your stack to initialize the SDK:
const Stack = Contentstack.Stack({ "api_key": "api_key", "delivery_token": "delivery_token", "environment": "environment" });
Once you have initialized the SDK, you can start getting content in your app.
Querying content from your stack
To get a single entry, you need to specify the content type as well as the ID of the entry.
const Stack = Contentstack.Stack({ "api_key": "api_key", "delivery_token": "delivery_token", "environment": "environment" }); const entry = Stack.ContentType('blog').Entry("entry_uid"); const result = await entry.fetch()
Note: We have a method on query for a specific language. For example, consider the following query:
Stack.ContentType('content_type_uid').Query().language('fr-fr').toJSON().find()
It will provide all entries of a content type published on the French locale.
To retrieve multiple entries of a content type, you need to specify the content type UID. You can also specify search parameters to filter results.
const Stack = Contentstack.Stack({"api_key": "api_key", "delivery_token": "delivery_token", "environment": "environment" }); const Query = Stack.ContentType('blog').Query(); Query.where("title", "welcome") .includeSchema() .includeCount() .toJSON() .find() .then((result) => { }) .catch((error))=> { });
- Currently, the JavaScript SDK does not support multiple content types referencing in a single query. For more information on how to query entries and assets, refer the Queries section of our Content Delivery API documentation.
- By default, the limit for response details per request is 100, with the maximum limit set at 250.
Paginating Responses
In a single instance, the Get Multiple Entries query will retrieve only the first 100 items of the specified content type. You can paginate and retrieve the rest of the items in batches using the skip and limit parameters in subsequent requests.
const Stack = Contentstack.Stack({ "api_key": "api_key", "delivery_token": "delivery_token", "environment": "environment" }); let blogQuery = Stack.ContentType('example').Query(); blogQuery.skip(20).limit(20).find() .then((result) => { }).catch((error))=> { });
Querying Assets from your stack
To get a single asset, you need to specify the UID of the asset.
const Stack = Contentstack.Stack({ "api_key": "api_key", "delivery_token": "delivery_token", "environment": "environment" }); const Asset = Stack.Asset("asset_uid"); Asset.fetch() .then((result) => { }).catch((error))=> { });
To retrieve multiple assets. You can also specify search parameters to filter results.
const Stack = Contentstack.Stack({ "api_key": "api_key", "delivery_token": "delivery_token", "environment": "environment" }); const Query = Stack.Asset().Query(); Query .limit(20) .toJSON() .find() .then(function success(result) { }, function error(err) { });
Cache Policies
You can set a cache policy on a stack and/or query object.
Setting a cache policy on a stack
This option allows you to globalize a cache policy. This means the cache policy you set will be applied to all the query objects of the stack.
//Setting a cache policy on a stack Stack.setCachePolicy(Contentstack.CachePolicy.NETWORK_ELSE_CACHE)
Setting a cache policy on a query object
This option allows you to set/override a cache policy on a specific query object.
// setting a cache policy on a queryobject Query.setCachePolicy(Contentstack.CachePolicy.CACHE_THEN_NETWORK)
Contentstack
Creates an instance of `Contentstack`.
Name | Type | Description |
---|---|---|
CachePolicy | Contentstack.CachePolicy | CachePolicy contains different cache policies constants. |
Stack
Name | Type | Description |
---|---|---|
api_key (required) | string | Stack API key |
delivery_token | string | Stack Delivery token. |
environment | string | Stack Environment name. |
region | ContentstackRegion | string | DB region for Stack. You can choose from five regions namely, NA, EU, Azure NA, Azure EU, and GCP NA. |
branch | string | Name of the branch you want to fetch data from |
live_preview | object | Live preview configuration. |
fetchOptions.debug | boolean | Optional, to enable debug logs set to true. |
fetchOptions.logHandler | function | A log handler function to process given log messages & errors. |
fetchOptions.agent | HttpProxyAgent | |
fetchOptions.timeout | number | Set timeout for the request. |
fetchOptions.retryLimit | number | The number of retries before failure. |
fetchOptions.retryDelay | number | The number of ms to use for operation retries. |
fetchOptions.retryCondition | function | A function to determine if the error can be retried. |
fetchOptions.retryDelayOptions.base | number | The base number of milliseconds to use in the exponential backoff for operation retries. |
fetchOptions.retryDelayOptions.customBackoff | number | A custom function that accepts a retry count and error and returns the amount of time to delay in milliseconds. |
Initialize Stack:
import Contentstack from 'contentstack'; const Stack = Contentstack.Stack({ api_key: 'api_key', delivery_token: 'delivery_token', environment: 'environment' });
To set the European, Azure North American, Azure European, or GCP North America region, refer to the code below:
import Contentstack from 'contentstack'; const Stack = new Contentstack({ 'api_key': "api_key", 'delivery_token': "delivery_token", 'environment': "environment", "region": Contentstack.Region.<<add_your_region>>})
For Setting the Branch for a Region.
import Contentstack from 'contentstack'; const Stack = Contentstack.Stack({ api_key: 'api_key', delivery_token: 'delivery_token', environment: 'environment', region: Contentstack.Region.<<add_your_region>>, host: '<<add_your_host_URL>>', branch: 'branch')
Proxy Configuration
const Stack = Contentstack.Stack({ api_key: 'api_key', delivery_token: 'delivery_token', environment: 'environment', URL: URL, fetchOptions: { fetch: `proxy-url` } });
Here are a few examples of how you can add a username and password to HttpProxyAgent.
- You can pass it in the URI:
var proxyAgent = new HttpsProxyAgent('https://username:[email protected]');
- You can set it in the auth option
var proxyOpts = url.parse('https://your-proxy.com'); proxyOpts.auth = 'username:password'; var proxyAgent = new HttpsProxyAgent(proxyOpts);
- You can even set the HTTP header manually:
var proxyOpts = url.parse('https://your-proxy.com'); proxyOpts.headers = { 'Proxy-Authentication': 'Basic ' + new Buffer('username:password').toString('base64') }; var proxyAgent = new HttpsProxyAgent(proxyOpts);
Plugins
When creating custom plugins, through this request, you can pass the detailss of your custom plugins. This facilitates their utilization in subsequent requests when retrieving details.
// custom class for plugin class CrossStackPlugin { onRequest (stack, request) { // request modifications return request } async onResponse (stack, request, response, data) { // response modifications here return response } } const Stack = Contentstack.Stack({ api_key, delivery_token, environment, plugins: [ new CrossStackPlugin(), new Livepreview() ] });
Stack
Name | Type | Description |
---|---|---|
param.api_key (required) | string | Stack API Key. |
param.delivery_token (required) | string | Stack Delivery token. |
param.environment (required) | string | Stack Environment name. |
setProtocol
Name | Type | Description |
---|---|---|
protocol | string |
import Contentstack from 'contentstack' const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); Stack.setProtocol('https');
setHost
Sets the host of the API server
Name | Type | Description |
---|---|---|
host | string | Sets the host of the API server |
import Contentstack from 'contentstack' const Stack = Contentstack.Stack({"api_key": "api_key", "delivery_token": "delivery_token", "environment": "environment"}); Stack.setHost('custom.contentstack.com');
setPort
Name | Type | Description |
---|---|---|
port | number |
import Contentstack from 'contentstack' const Stack = Contentstack.Stack({"api_key": "api_key", "delivery_token": "delivery_token", "environment": "environment"}); SStack.setPort(443);
setCachePolicy
Name | Type | Description |
---|---|---|
key | Contentstack.CachePolicy | Default: Contentstack.CachePolicy.ONLY_NETWORK |
import Contentstack from 'contentstack' const Stack = Contentstack.Stack({"api_key": "api_key", "delivery_token": "delivery_token", "environment": "environment"}); Stack.setCachePolicy(Contentstack.CachePolicy.IGNORE_CACHE);
setCacheProvider
import Contentstack from 'contentstack'; const Stack = Contentstack.Stack({"api_key": "api_key", "delivery_token": "delivery_token", "environment": "environment"}); Stack.setCacheProvider({ get: function (key, callback) { try { callback(null, <cache_provider>.get(key)); } catch(e) { callback(e); } }, set: function (key, value, callback) { try { if(key && value) <cache_provider>.set(key, value); callback(); } catch(e) { callback(e); } } });
getCacheProvider
import Contentstack from 'contentstack' const Stack = Contentstack.Stack({"api_key": "api_key", "delivery_token": "delivery_token", "environment": "environment"}); Stack.getCacheProvider();
Assets
Name | Type | Description |
---|---|---|
uid | string | UID of the asset |
To get all Assets from the stack:
import Contentstack from 'contentstack' const Stack = Contentstack.Stack({"api_key": "api_key", "delivery_token": "delivery_token", "environment": "environment"}); const result = await Stack;/Assets().Query().toJSON().find();
To specific Asset from the stack with uid:
import Contentstack from 'contentstack' const Stack = Contentstack.Stack({"api_key": "api_key", "delivery_token": "delivery_token", "environment": "environment"}); const result = await Stack.Assets('asset_uid').toJSON().find();
ContentType
Name | Type | Description |
---|---|---|
content_type_uid | string | UID of the existing content type |
import Contentstack from 'contentstack' const Stack = Contentstack.Stack({"api_key": "api_key", "delivery_token": "delivery_token", "environment": "environment"}); const result = await Stack.ContentType('content_type_uid').Query().toJSON().find()
getContentTypes
import Contentstack from 'contentstack' const Stack = Contentstack.Stack({"api_key": "api_key", "delivery_token": "delivery_token", "environment": "environment"}); Const result = await Stack.getContentTypes({"include_global_field_schema": true});
getLastActivities
import Contentstack from 'contentstack'; const Stack = Contentstack.Stack({"api_key": "api_key", "delivery_token": "delivery_token", "environment": "environment"}); const result = await Stack.getLastActivities().toJSON().fetch();
Taxonomies
Example:
import Contentstack from 'contentstack'; const stack = Contentstack.stack('api_key', 'delivery_token', 'environment'); let data; stack .Taxonomies() .where("taxonomies.taxonomy_uid", "term_uid") .toJSON() .find() .then(response => { // the response data = response; })
Query
import Contentstack from 'contentstack'; const Stack = Contentstack.Stack({"api_key": "api_key", "delivery_token": "delivery_token", "environment": "environment"}); const result = await Stack.ContentType('content_type_uid').Query().toJSON().find();
import Contentstack from 'contentstack'; const Stack = Contentstack.Stack({"api_key": "api_key", "delivery_token": "delivery_token", "environment": "environment"}); const result = await Stack.Assets().Query().toJSON().find();
imageTransform
Performs transformations on images of mentioned url based on transformation parameters
Name | Type | Description |
---|---|---|
url | string | Image url on which transformations need to be applied. |
params | object | Object with transformation parameters |
import Contentstack from 'contentstack' const Stack = Contentstack.Stack({"api_key": "api_key", "delivery_token": "delivery_token", "environment": "environment"}); Stack.imageTransform(imageURL, {height: 100, width: 200, disable: "upscale"});
import Contentstack from 'contentstack' const Stack = Contentstack.Stack({"api_key": "api_key", "delivery_token": "delivery_token", "environment": "environment"}); Stack.imageTransform(imageURL, {crop: "150,100"});
import Contentstack from 'contentstack' const Stack = Contentstack.Stack({"api_key": "api_key", "delivery_token": "delivery_token", "environment": "environment"}); Stack.imageTransform(imageURL, {format: "png", crop: "150,100"});
sync
Name | Type | Description |
---|---|---|
param.init | boolean | initializing sync |
param.locale | string | initializing sync with entries of a specific locale |
param.start_date | string | Default: initializing sync with entries published after a specific date |
param.content_type_uid | string | initializing sync with entries of a specific content type |
param.type | string | Use the type parameter to get a specific type of content.Supports 'asset_published', 'entry_published', 'asset_unpublished', 'entry_unpublished', 'asset_deleted', 'entry_deleted', 'content_type_deleted' |
param.pagination_token | string | Fetching the next batch of entries using pagination token |
param.sync_token | string | Performing subsequent sync after initial sync |
For initializing sync:
import Contentstack from 'contentstack' const Stack = Contentstack.Stack({"api_key": "api_key", "delivery_token": "delivery_token", "environment": "environment"}); const result = await Stack.sync({'init': true});
For initializing sync with entries of a specific locale:
import Contentstack from 'contentstack' const Stack = Contentstack.Stack({"api_key": "api_key", "delivery_token": "delivery_token", "environment": "environment"}); const result = await Stack.sync({'init': true, 'locale': 'en-us'});
For initializing sync with entries published after a specific date:
import Contentstack from 'contentstack' const Stack = Contentstack.Stack({"api_key": "api_key", "delivery_token": "delivery_token", "environment": "environment"}); const result = await Stack.sync({'init': true, 'start_date': '2018-10-22'});
For initializing sync with entries of a specific content type:
import Contentstack from 'contentstack' const Stack = Contentstack.Stack({"api_key": "api_key", "delivery_token": "delivery_token", "environment": "environment"}); const result = await Stack.sync({'init': true, 'content_type_uid': 'session'});
For initializing sync with specific type:
import Contentstack from 'contentstack' const Stack = Contentstack.Stack({"api_key": "api_key", "delivery_token": "delivery_token", "environment": "environment"}); const result = await Stack.sync({'init': true, 'type': 'entry_published'});
For fetching the next batch of entries using pagination token:
import Contentstack from 'contentstack' const Stack = Contentstack.Stack({"api_key": "api_key", "delivery_token": "delivery_token", "environment": "environment"}); const result = await Stack.sync({'pagination_token': '<page_token>'});
For performing subsequent sync after initial sync:
import Contentstack from 'contentstack' const Stack = Contentstack.Stack({"api_key": "api_key", "delivery_token": "delivery_token", "environment": "environment"}); const result = await Stack.sync({'sync_token': '<sync_token>'})
Assets
Creates an instance of `Assets`.
Retrieves all assets of a stack by default. To retrieve a single asset, specify its UID.
Name | Type | Description |
---|---|---|
uid | string | UID of asset you want to retrieve |
addParam
import Contentstack from 'contentstack' const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.Assets('asset_uid').addParam('include_dimension', 'true').toJSON().fetch()
toJSON
import Contentstack from 'contentstack' const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.ContentType('content_type_uid').Query().toJSON().find()
import Contentstack from 'contentstack' const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.Assets().Query().toJSON().find()
import Contentstack from 'contentstack' const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.ContentType('content_type_uid').Entry('entry_uid').toJSON().fetch()
import Contentstack from 'contentstack' const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.Assets('asset_uid').toJSON().fetch()
fetch
import Contentstack from 'contentstack' const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.Assets('asset_uid').addParam('include_dimension', 'true').toJSON().fetch()
where
The where() method retrieves the assets from the stack using any other field UID of the assets.
Name | Type | Description |
---|---|---|
field_name (required) | String | Field UID of the Asset |
value (required) | String | Value of the Asset |
Example:
import Contentstack from 'contentstack' const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.Assets().Query().where('field_name', "value").toJSON().find()
Entry
Name | Type | Description |
---|---|---|
uid (required) | string | Uid of the entry |
language
Name | Type | Description |
---|---|---|
language_code | string | language code. e.g. 'en-us', 'ja-jp', etc. |
import Contentstack from 'contentstack' const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.ContentType('content_type_uid').Entry('entry_uid').language('language_code').toJSON().fetch();
addParam
import Contentstack from 'contentstack' const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.ContentType('content_type_uid').Entry('entry_uid').addParam('include_count', 'true').toJSON().fetch()
addQuery
Adds query to Entry object
Name | Type | Description |
---|---|---|
key | string | Key of the query |
value | any | Value of the query |
import Contentstack from 'contentstack' const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.ContentType('content_type_uid').Entry('entry_uid').addQuery('include_content_type', true).toJSON().fetch();
only
Name | Type | Description |
---|---|---|
values | string|array |
The only function with field_uid will include the data of only the specified fields for each entry and exclude the data of all other fields.
import Contentstack from 'contentstack'; const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.ContentType('content_type_uid').Entry('entry_uid').only('title').toJSON().fetch();
The only function with an array of field_uids will include multiple fields for each entry and exclude the data of all other fields.
import Contentstack from 'contentstack'; const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.ContentType('content_type_uid').Entry('entry_uid').only(['title', 'description']).toJSON().fetch();
In only, we have the only with a reference parameter, where you need to enter the UID of the reference field in place of "reference_field_uid", and the second parameter to include the data of only the specified field_uid for each entry and exclude the data of all other fields.
import Contentstack from 'contentstack'; const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.ContentType('content_type_uid').Entry('entry_uid').includeReference('reference_field_uid').only('reference_field_uid','title').toJSON().fetch();
In only, we have the only with a reference parameter with an array, where you need to enter the UID of the reference field in place of "reference_field_uid", and the second parameter with an array of fields to include the data of only the specified array of field_uids for each entry and exclude the data of all other fields.
import Contentstack from 'contentstack'; const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.ContentType('content_type_uid').Entry('entry_uid').includeReference('reference_field_uid').only('reference_field_uid',['title', 'description']).toJSON().fetch();
except
Name | Type | Description |
---|---|---|
values | string|array |
The except function with field_uid will exclude the data of only the specified fields for each entry and includes the data of all other fields.
import Contentstack from 'contentstack'; const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.ContentType('content_type_uid').Entry('entry_uid').except('title').toJSON().fetch();
The except function with an array of field_uids will except multiple fields for each entry and include the data of all other fields.
import Contentstack from 'contentstack'; const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.ContentType('content_type_uid').Entry('entry_uid').except(['title', 'description']).toJSON().fetch();
In except, we have the only with a reference parameter, where you need to enter the UID of the reference field in place of "reference_field_uid", and the second parameter to except the data of only the specified field_uid for each entry and include the data of all other fields.
import Contentstack from 'contentstack'; const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.ContentType('content_type_uid').Entry('entry_uid').includeReference('reference_field_uid').except('reference_field_uid','title').toJSON().fetch();
In except, we have the only with a reference parameter with an array, where you need to enter the UID of the reference field in place of "reference_field_uid", and the second parameter with an array of fields to except the data of only the specified array of field_uids for each entry and include the data of all other fields.
import Contentstack from 'contentstack'; const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.ContentType('content_type_uid').Entry('entry_uid').includeReference('reference_field_uid').except('reference_field_uid',['title', 'description']).toJSON().fetch();
includeReference
Fetches the entire content of referenced entry.
Name | Type | Description |
---|---|---|
value | string|array |
Include Reference with reference_field_uids as array:
import Contentstack from 'contentstack' const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.ContentType('content_type_uid').Entry('entry_uid').includeReference(['reference_field_uid','other_reference_field_uid']).toJSON().fetch();
Include Reference with reference_field_uids and its children reference
import Contentstack from 'contentstack'; const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.ContentType('content_type_uid').Entry('entry_uid').includeReference(['reference_field_uid', 'reference_field_uid.child_reference_field_uid']).toJSON().fetch();
Include Reference with reference_field_uid:
import Contentstack from 'contentstack'; const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.ContentType('content_type_uid').Entry('entry_uid').includeReference('reference_field_uid').toJSON().fetch();
includeEmbeddedItems
import Contentstack from 'contentstack' const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.ContentType('content_type_uid').Entry('entry_uid').includeEmbeddedItems().toJSON().fetch();
includeContentType
import Contentstack from 'contentstack' const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.ContentType('content_type_uid').Entry('entry_uid').includeContentType().toJSON().fetch();
includeBranch
import Contentstack from 'contentstack' const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.ContentType('content_type_uid').Entry('entry_uid').includeBranch().toJSON().fetch();
includeFallback
import Contentstack from 'contentstack' const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); Stack.ContentType('content_type_uid;).Entry('entry_uid').includeFallback().fetch()
Taxonomy
Taxonomy helps you categorize pieces of content within your stack to facilitate easy navigation and retrieval of information.
Note: All methods in the Query section are applicable for taxonomy-based filtering as well.
equalAndBelow
The equalAndBelow method retrieves all entries for a specific taxonomy that match a specific term and all its descendant terms, requiring only the target term.
Note: This query is applicable for the stack.Taxonomies() and stack.ContentType('uid').Query() methods.
Name | Type | Description |
---|---|---|
key (required) | string | UID of the taxonomy field, specified as "taxonomies.<taxonomy_uid>" |
value (required) | string | UID of the term to match or compare |
levels | int | Depth level till which terms will be matched |
Example:
let data; stack .ContentType('ct_uid') .Query() .equalAndBelow("taxonomies.taxonomy_uid", "term_uid", 3) .toJSON() .find() .then(response => { // the response data = response; })
below
The below method retrieves all entries for a specific taxonomy that match all of their descendant terms by specifying only the target term and a specific level.
Note: If you don't specify the level, the default behavior is to retrieve terms up to level 10.
Name | Type | Description |
---|---|---|
key (required) | string | UID of the taxonomy field, specified as "taxonomies.<taxonomy_uid>" |
value (required) | string | UID of the term to match or compare |
levels | int | Depth level till which terms will be matched |
Example:
let data; stack .ContentType('ct_uid') .Query() .below("taxonomies.taxonomy_uid", "term_uid", 3) .toJSON() .find() .then(response => { // the response data = response; })
equalAndAbove
The equalAndAbove method retrieves all entries for a specific taxonomy that match a specific term and all its ancestor terms, requiring only the target term and a specified level.
Note: If you don't specify the level, the default behavior is to retrieve terms up to level 10.
Name | Type | Description |
---|---|---|
key (required) | string | UID of the taxonomy field, specified as "taxonomies.<taxonomy_uid>" |
value (required) | string | UID of the term to match or compare |
levels | int | Depth level till which terms will be matched |
Example:
let data; stack .Taxonomies() .equalAndAbove("taxonomies.taxonomy_uid", "term_uid", 3) .toJSON() .find() .then(response => { // the response data = response; })
above
The above method retrieves all entries for a specific taxonomy that match only the parent term(s) of a specified target term, excluding the target term itself and a specified level.
Note: If you don't specify the level, the default behavior is to retrieve terms up to level 10.
Name | Type | Description |
---|---|---|
key (required) | string | UID of the taxonomy field, specified as "taxonomies.<taxonomy_uid>" |
value (required) | string | UID of the term to match or compare |
levels | int | Depth level till which terms will be matched |
Example:
let data; stack .ContentType('ct_uid') .Query() .above("taxonomies.taxonomy_uid", "term_uid", 3) .toJSON() .find() .then(response => { // the response data = response; })
Query
An initializer is responsible for creating Query object. Provides support for all search queries
addParam
import Contentstack from 'contentstack' const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.ContentType('content_type_uid').Query().addParam('include_count', 'true').toJSON().find()
notEqualTo
Name | Type | Description |
---|---|---|
key | string | UID of the field |
value | any | Value used to match or compare |
import Contentstack from 'contentstack'; const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.ContentType('content_type_uid').Query().notEqualTo('title', 'some random title').toJSON().find();
containedIn
Name | Type | Description |
---|---|---|
key | string | UID of the field |
value | array | Array of values that are to be used to match or compare |
import Contentstack from 'contentstack'; const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.ContentType('content_type_uid').Query().containedIn('title', ['Demo', 'Welcome']).toJSON().find();
notContainedIn
Name | Type | Description |
---|---|---|
key | string | UID of the field |
value | array | Array of values that are to be used to match or compare |
import Contentstack from 'contentstack'; const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.ContentType('content_type_uid').Query().notContainedIn('title', ['Demo', 'Welcome']).toJSON().find();
and
Name | Type | Description |
---|---|---|
queries | array | array of Query objects or raw queries |
and with Query instances:
import Contentstack from 'contentstack'; const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const Query1 = Stack.ContentType('content_type_1').Query().where('title', 'demo'); const Query2 = Stack.ContentType('content_type_1').Query().lessThan('age', 30); const result = await Stack.ContentType('content_type_uid').Query().and(Query1, Query2).toJSON().find();
and with raw queries:
import Contentstack from 'contentstack'; const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const Query1 = Stack.ContentType('content_type_1').Query().where('title', 'demo').getQuery(); const Query2 = Stack.ContentType('content_type_1').Query().lessThan('age', 30).getQuery(); const result = await Stack.ContentType('content_type_uid').Query().and(Query1, Query2).toJSON().find();
or
Name | Type | Description |
---|---|---|
queries | array | array of Query objects or raw queries |
or with Query instances:
import Contentstack from 'contentstack'; const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const Query1 = Stack.ContentType('content_type_1').Query().where('title', 'demo'); const Query2 = Stack.ContentType('content_type_1').Query().lessThan('age', 30); const result = await Stack.ContentType('content_type_uid').Query().or(Query1, Query2).toJSON().find();
or with raw queries:
import Contentstack from 'contentstack'; const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const Query1 = Stack.ContentType('content_type_1').Query().where('title', 'demo').getQuery(); const Query2 = Stack.ContentType('content_type_1').Query().lessThan('age', 30).getQuery(); const result = await Stack.ContentType('content_type_uid').Query().or(Query1, Query2).toJSON().find();
lessThan
Name | Type | Description |
---|---|---|
key | string | UID of the field |
value | any | Value used to match or compare |
import Contentstack from 'contentstack'; const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.ContentType('content_type_uid').Query().lessThan('age', 20).toJSON().find();
lessThanOrEqualTo
Name | Type | Description |
---|---|---|
key | string | UID of the field |
value | any | Value used to match or compare |
import Contentstack from 'contentstack'; const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.ContentType('content_type_uid').Query().lessThanOrEqualTo('age', 20).toJSON().find();
greaterThanOrEqualTo
Name | Type | Description |
---|---|---|
key | string | UID of the field |
value | any | Value used to match or compare |
import Contentstack from 'contentstack'; const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.ContentType('content_type_uid').Query().greaterThanOrEqualTo('age', 20).toJSON().find();
limit
Name | Type | Description |
---|---|---|
limit | number | maximum number of entries to be returned |
import Contentstack from 'contentstack'; const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.ContentType('content_type_uid').Query().limit(20).toJSON().find();
ascending
Name | Type | Description |
---|---|---|
key | string | field uid based on which the ordering will be done |
import Contentstack from 'contentstack'; const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.ContentType('content_type_uid').Query().ascending('field_uid').toJSON().find();
descending
Name | Type | Description |
---|---|---|
key | string | field uid based on which the ordering will be done |
import Contentstack from 'contentstack'; const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.ContentType('content_type_uid').Query().descending('field_uid').toJSON().find();
exists
Name | Type | Description |
---|---|---|
key | string | UID of the field |
import Contentstack from 'contentstack'; const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.ContentType('content_type_uid').Query().exists('field_uid').toJSON().find();
notExists
Name | Type | Description |
---|---|---|
key | string | UID of the field |
import Contentstack from 'contentstack'; const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.ContentType('content_type_uid').Query().notExists('field_uid').toJSON().find();
only
Name | Type | Description |
---|---|---|
values | string|array |
The only function with field_uid will include the data of only the specified fields for each entry and exclude the data of all other fields.
import Contentstack from 'contentstack'; const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.ContentType('content_type_uid').Query().only('title').toJSON().find();
The only function with an array of field_uids will include multiple fields for each entry and exclude the data of all other fields.
import Contentstack from 'contentstack'; const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.ContentType('content_type_uid').Query().only(['title', 'description']).toJSON().find();
In only, we have the only with a reference parameter, where you need to enter the UID of the reference field in place of "reference_field_uid", and the second parameter to include the data of only the specified field_uid for each entry and exclude the data of all other fields.
import Contentstack from 'contentstack'; const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.ContentType('content_type_uid').Query().includeReference('reference_field_uid').only('reference_field_uid','title').toJSON().find();
In only, we have the only with a reference parameter with an array, where you need to enter the UID of the reference field in place of "reference_field_uid", and the second parameter with an array of fields to include the data of only the specified array of field_uids for each entry and exclude the data of all other fields.
import Contentstack from 'contentstack'; const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.ContentType('content_type_uid').Query().includeReference('reference_field_uid').only('reference_field_uid',['title', 'description']).toJSON().find();
except
Name | Type | Description |
---|---|---|
values | string|array |
The except function with field_uid will exclude the data of only the specified fields for each entry and includes the data of all other fields.
import Contentstack from 'contentstack'; const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.ContentType('content_type_uid').Query().except('title').toJSON().find();
The except function with an array of field_uids will except multiple fields for each entry and include the data of all other fields.
import Contentstack from 'contentstack'; const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.ContentType('content_type_uid').Query().except(['title', 'description']).toJSON().find();
In except, we have the only with a reference parameter, where you need to enter the UID of the reference field in place of "reference_field_uid", and the second parameter to except the data of only the specified field_uid for each entry and include the data of all other fields.
import Contentstack from 'contentstack'; const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.ContentType('content_type_uid').Query().includeReference('reference_field_uid').except('reference_field_uid','title').toJSON().find();
In except, we have the only with a reference parameter with an array, where you need to enter the UID of the reference field in place of "reference_field_uid", and the second parameter with an array of fields to except the data of only the specified array of field_uids for each entry and include the data of all other fields.
import Contentstack from &'contentstack'; const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.ContentType('content_type_uid').Query().includeReference('reference_field_uid').except('reference_field_uid',['title', 'description']).toJSON().find();
regex
Name | Type | Description |
---|---|---|
key | string | uid of the field |
value | any | value used to match or compare |
options | any | match or compare value in entry |
regex without options
import Contentstack from 'contentstack'; const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.ContentType('content_type_uid').Query().regex('title', '^Demo').toJSON().find();
regex with options
import Contentstack from 'contentstack'; const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.ContentType('content_type_uid').Query().regex('title', '^Demo', 'i').toJSON().find();
tags
Name | Type | Description |
---|---|---|
values | array | array of tags |
import Contentstack from 'contentstack'; const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.ContentType('content_type_uid').Query().tags(['technology', 'business']).toJSON().find();
includeReference
Fetches the entire content of referenced entry.
Name | Type | Description |
---|---|---|
value | string|array |
Include Reference with reference_field_uids as array:
import Contentstack from 'contentstack' const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.ContentType('content_type_uid').Query().includeReference(['reference_field_uid','other_reference_field_uid']).toJSON().find();
Include Reference with reference_field_uids and its children reference
import Contentstack from 'contentstack'; const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.ContentType('content_type_uid').Query().includeReference(['reference_field_uid', 'reference_field_uid.child_reference_field_uid']).toJSON().find();
Include Reference with reference_field_uid:
import Contentstack from 'contentstack'; const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.ContentType('content_type_uid').Query().includeReference('reference_field_uid').toJSON().find();
referenceIn
Name | Type | Description |
---|---|---|
fieldUid | string | Reference field Uid |
query | object | RAW (JSON) queries |
referenceIn with Query instances
import Contentstack from 'contentstack'; const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const Query1 = Stack.ContentType('content_type_1').Query().where('title', 'Demo'); const result = await Stack.ContentType('content_type_uid').Query().referenceIn('brand', Query1).toJSON().find();
referenceIn with raw queries:
import Contentstack from 'contentstack'; const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.ContentType('content_type_uid').Query().referenceIn('brand', {'title': 'Demo'}).toJSON().find();
referenceNotIn
Name | Type | Description |
---|---|---|
fieldUid | string | Reference field Uid |
query | object | RAW (JSON) queries |
referenceNotIn with Query instances:
import Contentstack from 'contentstack'; const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const Query1 = Stack.ContentType('content_type_1').Query().where('title', 'Demo'); const result = await Stack.ContentType('content_type_uid').Query().referenceNotIn('brand', Query1).toJSON().find();
referenceNotIn with raw queries:
import Contentstack from 'contentstack'; const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.ContentType('content_type_uid').Query().referenceNotIn('brand', {'title': 'Demo'}).toJSON().find();
includeReferenceContentTypeUID
import Contentstack from 'contentstack'; const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.ContentType('content_type_uid').Query().includeReferenceContentTypeUID().toJSON().find();
includeCount
import Contentstack from 'contentstack'; const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.ContentType('content_type_uid').Query().includeCount().toJSON().find();
includeContentType
import Contentstack from 'contentstack' const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.ContentType('content_type_uid').Query().includeContentType().toJSON().find();
includeBranch
import Contentstack from 'contentstack' const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.ContentType('content_type_uid').Query().includeBranch().toJSON().find();
includeEmbeddedItems
import Contentstack from 'contentstack' const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.ContentType('content_type_uid').Query().includeEmbeddedItems().toJSON().find();
includeFallback
import Contentstack from 'contentstack' const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); Stack.ContentType('content_type_uid;).Query().includeFallback().fetch()
query
Name | Type | Description |
---|---|---|
query | object | RAW (JSON) queries |
import Contentstack from 'contentstack'; const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.ContentType('content_type_uid').Query().query({'brand': {'$nin_query': {'title': 'Apple Inc.'}}}).toJSON().find();
addQuery
Adds query to Entry object
Name | Type | Description |
---|---|---|
key | string | Key of the query |
value | any | Value of the query |
import Contentstack from 'contentstack' const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.ContentType('content_type_uid').Query().addQuery('include_content_type', true).toJSON().find();
getQuery
import Contentstack from 'contentstack'; const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const rawQuery = await Stack.ContentType('content_type_uid').Query().where('title', 'Demo').getQuery();
language
Name | Type | Description |
---|---|---|
language_code | string | language code. e.g. 'en-us', 'ja-jp', etc. |
import Contentstack from 'contentstack'; const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.ContentType('content_type_uid').Query().language('language_code').toJSON().find();
find
Name | Type | Description |
---|---|---|
param | object | Fetch options to fetch particular query |
import Contentstack from 'contentstack'; const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.ContentType('content_type_uid').Query().toJSON().find();
findOne
Name | Type | Description |
---|---|---|
param | object | Fetch options to fetch particular query |
import Contentstack from 'contentstack'; const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.ContentType('content_type_uid').Query().toJSON().findOne();
count
import Contentstack from 'contentstack'; const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'}); const result = await Stack.ContentType('content_type_uid').Query().count().toJSON().find();
equalAndBelow
The equalAndBelow method retrieves all entries for a specific taxonomy that match a specific term and all its descendant terms, requiring only the target term.
Note: This query is applicable for the stack.Taxonomies() and stack.ContentType('uid').Query() methods.
Name | Type | Description |
---|---|---|
key (required) | string | UID of the taxonomy field, specified as taxonomies.<taxonomy_uid> |
value (required) | string | UID of the term to match or compare |
levels | int | Depth level till which terms will be matched |
Example:
let data; stack .ContentType('ct_uid') .Query() .equalAndBelow("taxonomies.taxonomy_uid", "term_uid", 3) .toJSON() .find() .then(response => { // the response data = response; })
below
The below method retrieves all entries for a specific taxonomy that match all of their descendant terms by specifying only the target term and a specific level.
Note: If you don't specify the level, the default behavior is to retrieve terms up to level 10.
Name | Type | Description |
---|---|---|
key (required) | string | UID of the taxonomy field, specified as taxonomies.<taxonomy_uid> |
value (required) | string | UID of the term to match or compare |
levels | int | Depth level till which terms will be matched |
Example:
let data; stack .ContentType('ct_uid') .Query() .below("taxonomies.taxonomy_uid", "term_uid", 3) .toJSON() .find() .then(response => { // the response data = response; })
equalAndAbove
The equalAndAbove method retrieves all entries for a specific taxonomy that match a specific term and all its ancestor terms, requiring only the target term and a specified level.
Note: If you don't specify the level, the default behavior is to retrieve terms up to level 10.
Name | Type | Description |
---|---|---|
key (required) | string | UID of the taxonomy field, specified as taxonomies.<taxonomy_uid> |
value (required) | string | UID of the term to match or compare |
levels | int | Depth level till which terms will be matched |
Example:
let data; stack .Taxonomies() .equalAndAbove("taxonomies.taxonomy_uid", "term_uid", 3) .toJSON() .find() .then(response => { // the response data = response; })
above
The above method retrieves all entries for a specific taxonomy that match only the parent term(s) of a specified target term, excluding the target term itself and a specified level.
Note: If you don't specify the level, the default behavior is to retrieve terms up to level 10.
Name | Type | Description |
---|---|---|
key (required) | string | UID of the taxonomy field, specified as taxonomies.<taxonomy_uid> |
value (required) | string | UID of the term to match or compare |
levels | int | Depth level till which terms will be matched |
Example:
let data; stack .ContentType('ct_uid') .Query() .above("taxonomies.taxonomy_uid", "term_uid", 3) .toJSON() .find() .then(response => { // the response data = response; })