Skip to content

getContent

The getContent function retrieves a single content from a project. This function is useful for fetching a specific piece of content, such as an article or blog post.

Example:

// Using the previously created client
const content = await client.getContent({ id: '1' });
console.log(content);

Interface

The getContent function accepts a required QueryParams object. It returns a promise that resolves to the content.

export interface ApiClient {
getContent: <Model>(queryParam: QueryParams<Model>) => Promise<Model>;
// ... other methods
}

Parameters

  • queryParam: An object containing query parameters to filter the content. This parameter is required to retrieve the content filtered by id or any other filter you want to apply.
interface QueryParams {
id?: Filter;
contentType?: Filter;
language?: Filter;
`fields.${string}`?: Filter; // Allows filtering by specific field values
// For example: 'fields.slug': 'my-post'
}
NameDescription
idThe ID of the content you want to retrieve.
This parameter must be a string.
Example: client.getContent({ id: '1' })
You can find the content id in your project’s Content tab.
contentTypeThe model of the content you want to retrieve.
Example: client.getContent({ id: '1', contentType: 'post' })
languageThe language of the content you want to retrieve.
This filter ensures only the model fields in the selected language are returned.
Example: client.getContent({ id: '1', language: 'es' })
fields.${string}Allows filtering by specific field values.
Examples:
client.getContent({ 'fields.slug': 'my-post' } })
client.getContent({ 'fields.title': { in: ['Hello World', 'Hola Mundo'] } })
Retrieves content with fields matching the specified values.
type Filter =
| string
| {
in?: string[];
};
NameDescription
stringFilters the content where the parameter equals this value.
Example: client.getContent({ id: '1', contentType: 'post' })
Retrieves a content item whose model equals post.
inFilters the content where the parameter matches any of the provided values.
Example: client.getContent({ id: '1', language: { in: ['es', 'en'] } })
Retrieves a content item with fields matching es and/or en.

Output

The getContent function returns an object representing the content (filtered or not). This object contains the content values that an users define in their project in Content Island.

Example:

import { createClient } from '@content-island/api-client';
// ... client initialization code
// Your Post model defined in Content Island
interface Post {
id: string;
title: string;
body: string;
order: number;
language: 'es' | 'en';
}
const post = await client.getContent<Post>({ id: '1' });
console.log(post);
/*
{
id: '1',
title: 'Hola Mundo',
body: 'Este es el cuerpo del post en markdown.',
order: 1,
language: 'es',
}
*/

As you can see, the content is already mapped to its corresponding model and you can access fields directly without manually mapping them.

getContent uses the mapContentToModel function under the hood to map the content to its model, visit that section for more information on how content mapping works.