The useStatamicApi composable is used to provide a comprehensive API interface for interacting with Statamic CMS.
It offers methods to fetch entries, navigations, globals, forms, taxonomies etc.
The composable by default, handles the following:
useStatamicPreviewModeuseStatamicSite (multi-tenancy or multi-lingual)$api instance is not available (e.g., used before plugin initialization).
[nuxt-statamic]: '`useStatamicApi` is used before the `statamic:plugin` was able to provide the `$api` e.g. custom fetch instance'
function useStatamicApi() {
return {
entries: fetchEntries, // Fetch entries from a specified collection.
entry: fetchEntry, // Fetch a specific entry by its ID from a collection.
entryByUri: fetchEntryByUri, // Fetch an entry by its URI (URL path).
collection: fetchCollection, // Fetch a collection tree by its handle.
navigations: fetchNavigations, // Fetch all configured navigations asynchronously.
navigation: fetchNavigation, // Fetch a specific navigation by its handle.
globals: fetchGlobals, // Fetch all globals for the current site.
global: fetchGlobal, // Fetch a specific global by its handle.
forms: fetchForms, // Fetch all available forms.
form: fetchForm, // Fetch a specific form by its handle.
submitForm, // Submit form data to a specific form.
taxonomyTerms: fetchTaxonomyTerms, // Fetch terms from a specific taxonomy.
taxonomyTerm: fetchTaxonomyTerm, // Fetch a specific taxonomy term by its slug.
graphql: fetchGraphql, // Execute a GraphQL query against the Statamic API.
redirects: fetchRedirects, // Fetch all redirects for the current site.
sitemapUrls: fetchSitemapUrls, // Fetch the sitemap URLs.
};
};
entries()Fetches entries from a specified collection.
<script setup lang="ts">
const api = useStatamicApi();
const { data: blogs } = await useAsyncData(
useStatamicCacheKey("entries", "blogs"),
() => api.entries("blogs")
);
</script>
<script setup lang="ts">
const api = useStatamicApi();
const { data: blogs } = await useAsyncData(
useStatamicCacheKey("entries", "blogs"),
() => api.entries("blogs", {
fields: ["title", "content", "slug", "author"],
order: "sort",
limit: 200,
}),
);
</script>
function fetchEntries<DataType extends Record<string, any> = StatamicApiEntryDefaultFields>(
collection: string,
params: Partial<StatamicApiParamsMultiple<DataType>> = {},
fetchOptions: Omit<FetchOptions, "params"> = {},
): Promise<StatamicApiResponseMultiple<DataType>>;
entry()Fetches a specific entry by its ID from a collection.
<script setup lang="ts">
const linkedBlog = computed(() => page.value?.linked_blog);
const api = useStatamicApi();
const { data: blog } = await useAsyncData(
useStatamicCacheKey("entry", "blog", linkedBlog.value?.id),
() => api.entry("blogs", linkedBlog.value?.id),
);
</script>
function fetchEntry<DataType extends Record<string, any> = StatamicApiEntryDefaultFields>(
collection: string,
entryId: string,
params: Partial<StatamicApiParamsSingle<DataType>> = {},
fetchOptions: Omit<FetchOptions, "params"> = {},
): Promise<StatamicApiResponseSingle<DataType>>;
entryByUri()Fetches an entry by its URI (URL path).
<script setup lang="ts">
const route = useRoute();
const api = useStatamicApi();
const {data: styleguideData} = await useAsyncData(
useStatamicCacheKey('entry', 'page', route.path),
() => api.entryByUri({uri: route.path}),
);
</script>
function fetchEntryByUri<DataType extends Record<string, any> = StatamicApiEntryDefaultFields>(
params: Partial<StatamicApiParamsUri<DataType>> = {},
fetchOptions: Omit<FetchOptions, "params"> = {},
): Promise<StatamicApiResponseSingle<DataType>>;
collection()Fetches a collection tree by its handle.
<script setup lang="ts">
const api = useStatamicApi();
const {data: styleguideData} = await useAsyncData(
useStatamicCacheKey('collection', 'blogs'),
() => api.collection('blogs'),
);
</script>
function fetchCollection<DataType extends Record<string, any> = StatamicApiEntryDefaultFields>(
collectionHandle: string,
params: Partial<StatamicApiParamsTree<DataType>> = {},
fetchOptions: Omit<FetchOptions, "params"> = {},
): Promise<StatamicApiResponseTree<DataType>>;
navigations()Fetches all configured navigations asynchronously.
This method fetches all navigations defined in the Statamic runtime config and returns them as a keyed object where each key is the navigation handle.
This is by default a server side method and is therefore cached. When you need to fetch the navigations on the client side, you can provide the baseURL option to the fetchOptions parameter to use the client side route instead.
<script setup lang="ts">
const api = useStatamicApi();
const { data } = await useAsyncData(
useStatamicCacheKey("navigations", "all"),
() => api.navigations()
);
</script>
<script setup lang="ts">
const api = useStatamicApi();
const { data } = await useAsyncData(
useStatamicCacheKey("navigations", "all"),
() => api.navigations({}, { baseURL: process.env.NUXT_PUBLIC_STATAMIC_URL }),
);
</script>
async function fetchNavigations<DataType extends Record<string, any>>(
params: Partial<StatamicApiParamsTree<DataType>> = {},
fetchOptions: Omit<FetchOptions, "params"> = {},
);
navigation()Fetches a specific navigation by its handle.
This is by default a server side method and is therefore cached. When you need to fetch the navigation on the client side, you can provide the baseURL option to the fetchOptions parameter to use the client side route instead.
<script setup lang="ts">
const api = useStatamicApi();
const { data } = await useAsyncData(
useStatamicCacheKey("navigation", "header"),
() => api.navigation("header")
);
</script>
<script setup lang="ts">
const api = useStatamicApi();
const { data } = await useAsyncData(
useStatamicCacheKey("navigation", "header"),
() => api.navigation("header", {}, { baseURL: process.env.NUXT_PUBLIC_STATAMIC_URL }),
);
</script>
function fetchNavigation<DataType extends Record<string, any>>(
navigationHandle: string,
params: Partial<StatamicApiParamsTree<DataType>> = {},
fetchOptions: Omit<FetchOptions, "params"> = {},
): Promise<StatamicApiResponseTree<DataType>>;
globals()Fetches all globals for the current site.
The response is automatically transformed to return an object where each key is the global handle, making it easier to access specific globals.
This is by default a server side method and is therefore cached. When you need to fetch the globals on the client side, you can provide the baseURL option to the fetchOptions parameter to use the client side route instead.
<script setup lang="ts">
const api = useStatamicApi();
const { data } = await useAsyncData(
useStatamicCacheKey("globals", "all"),
() => api.globals()
);
</script>
<script setup lang="ts">
const api = useStatamicApi();
const { data } = await useAsyncData(
useStatamicCacheKey("globals", "all"),
() => api.globals({}, { baseURL: process.env.NUXT_PUBLIC_STATAMIC_URL }),
);
</script>
function fetchGlobals<DataType extends Record<string, any>>(
params: Partial<Pick<StatamicApiParamsTree<DataType>, "site">> = {},
fetchOptions: Omit<FetchOptions, "params"> = {},
): Promise<Record<string, any>>;
global()Fetches a specific global by its handle.
This is by default a server side method and is therefore cached. When you need to fetch the global on the client side, you can provide the baseURL option to the fetchOptions parameter to use the client side route instead.
<script setup lang="ts">
const api = useStatamicApi();
const { data } = await useAsyncData(
useStatamicCacheKey("global", "site"),
() => api.global("site")
);
</script>
<script setup lang="ts">
const api = useStatamicApi();
const { data } = await useAsyncData(
useStatamicCacheKey("global", "site"),
() => api.global("site", {}, { baseURL: process.env.NUXT_PUBLIC_STATAMIC_URL }),
);
</script>
function fetchGlobal<DataType extends Record<string, any>>(
globalHandle: string,
params: Partial<Pick<StatamicApiParamsTree<DataType>, "site">> = {},
fetchOptions: Omit<FetchOptions, "params"> = {},
): Promise<StatamicApiResponseSingle<DataType>>;
forms()Fetches all available forms.
<script setup lang="ts">
const api = useStatamicApi();
const { data } = await useAsyncData(
useStatamicCacheKey("forms", "all"),
() => api.forms(),
);
</script>
function fetchForms(
fetchOptions: FetchOptions = {},
): Promise<Record<string, any>>;
form()Fetches a specific form by its handle.
<script setup lang="ts">
const api = useStatamicApi();
const { data } = await useAsyncData(
useStatamicCacheKey("form", "contact"),
() => api.form("contact"),
);
</script>
function fetchForm(
formHandle: string,
fetchOptions: FetchOptions = {},
): Promise<Record<string, any>>;
submitForm()Submits form data to a specific form.
function submitForm(
formHandle: string,
data: Record<string, any>,
fetchOptions: FetchOptions = {},
): Promise<Record<string, any>>;
taxonomyTerms()Fetches terms from a specific taxonomy.
<script setup lang="ts">
const api = useStatamicApi();
const { data } = await useAsyncData(
useStatamicCacheKey("taxonomyTerms", "tags"),
() => api.taxonomyTerms("tags"),
);
</script>
<script setup lang="ts">
const api = useStatamicApi();
const { data } = await useAsyncData(
useStatamicCacheKey("taxonomyTerms", "tags"),
() => api.taxonomyTerms("tags", { fields: ["title"] }),
);
</script>
function fetchTaxonomyTerms<DataType extends Record<string, any> = any>(
taxonomyHandle: string,
params: Partial<StatamicApiParamsMultiple<DataType>> = {},
fetchOptions: Omit<FetchOptions, "params"> = {},
): Promise<StatamicApiResponseSingle<DataType>[]>;
taxonomyTerm()Fetches a specific taxonomy term by its slug.
<script setup lang="ts">
const api = useStatamicApi();
const { data } = await useAsyncData(
useStatamicCacheKey("taxonomyTerm", "tags", "example-tag-1"),
() => api.taxonomyTerm("tags", "example-tag-1"),
);
</script>
function fetchTaxonomyTerm<DataType extends Record<string, any> = any>(
taxonomyHandle: string,
termSlug: string,
params: Partial<StatamicApiParamsSingle> = {},
fetchOptions: Omit<FetchOptions, "params"> = {},
): Promise<StatamicApiResponseSingle<DataType>>;
graphql()Executes a GraphQL query against the Statamic API.
<script setup lang="ts">
const api = useStatamicApi();
const { data } = await useAsyncData(
useStatamicCacheKey("graphql", "ping"),
() => api.graphql("MyQuery { ping }"),
);
</script>
function fetchGraphql(
query: string,
fetchOptions: Omit<FetchOptions, "query"> = {},
): Promise<Record<string, any>>;
redirects()Fetches all redirects for the current site.
This is by default a server side method and is therefore cached. When you need to fetch the redirects on the client side, you can provide the baseURL option to the fetchOptions parameter to use the client side route instead.
<script setup lang="ts">
const api = useStatamicApi();
const { data } = await useAsyncData(
useStatamicCacheKey("redirects", "all"),
() => api.redirects(),
);
</script>
<script setup lang="ts">
const api = useStatamicApi();
const { data } = await useAsyncData(
useStatamicCacheKey("redirects", "all"),
() => api.redirects({}, { baseURL: process.env.NUXT_PUBLIC_STATAMIC_URL }),
);
</script>
function fetchRedirects(
params: Record<string, any> = {},
fetchOptions: Omit<FetchOptions, "params"> = {},
): Promise<StatamicApiResponseRedirects>;
sitemapUrls()Fetches the sitemap URLs.
This is by default a server side method and is therefore cached. When you need to fetch the sitemap URLs on the client side, you can provide the baseURL option to the fetchOptions parameter to use the client side route instead.
Do note that you will most likely need to do some additional processing to the data to get the sitemap URLs in the correct format, since it will return a XML sitemap response.
<script setup lang="ts">
const api = useStatamicApi();
const { data } = await useAsyncData(
useStatamicCacheKey("sitemapUrls", "all"),
() => api.sitemapUrls(),
);
</script>
<script setup lang="ts">
const api = useStatamicApi();
const { data } = await useAsyncData(
useStatamicCacheKey("sitemapUrls", "all"),
() => api.sitemapUrls({ baseURL: process.env.NUXT_PUBLIC_STATAMIC_URL }),
);
</script>
function fetchSitemapUrls(
fetchOptions: FetchOptions = {},
): Promise<StatamicSitemapUrl[]>;
All the available parameters for the Statamic API, for the different types of requests. You can find which type of parameters are used for each request in the API Reference above.
{ fields: ["id", "title", "content"] }
{field_name}:{condition}={value}| Condition | Description |
|---|---|
is / equals | Include if field is equal to value. |
not / isnt | Include if field is not equal to value. |
exists / isset | Include if field exists. |
doesnt_exist / is_empty / null | Include if field doesn't exist. |
contains | Include if field contains value. |
doesnt_contain | Include if field doesn't contain value. |
in | Include if field value is in the provided array. |
not_in | Include if field value is not_in the provided array. |
starts_with | Include if field starts with value. |
doesnt_start_with | Include if field doesn't start with value. |
ends_with | Include if field ends with value. |
doesnt_end_with | Include if field doesn't end with value. |
gt | Include if field is greater than value. |
gte | Include if field is greater than or equal to value. |
lt | Include if field is less than value. |
lte | Include if field is less than or equal to value. |
matches / regex | Include if field matches case insensitive regex. |
doesnt_match | Include if field doesn't match case insensitive regex. |
is_alpha | Include if field contains only alphabetical characters. |
is_numeric | Include if field contains only numeric characters. |
is_alpha_numeric | Include if field contains only alphanumeric characters. |
is_url | Include if field is a valid URL. |
is_embeddable | Include if field is an embeddable video URL. |
is_email | Include if field is valid email address. |
is_after | Include if field is after date. |
is_before | Include if field is before date. |
is_numberwang | Include if field is numberwang. |
{ filter: { "title:contains": "awesome", featured: true, } }
{ sort: "fieldName" }
{ sort: "fieldName,anotherfield" }
{ sort: "fieldNameOne,-fieldNameTwo,fieldNameThree" }
{ sort: "-fieldName" }
{ sort: "fieldName->nestedFieldName" }
25 - The number of results to fetch per page (paginated).1 - The page number to fetch.useStatamicSite() composable inside the useStatamicApi() composable.useStatamicPreviewMode() composable inside the useStatamicApi() composable.