Usage
Basic Usage
Learn how to use the Js Craft CMS API package.
buildCraftQueryUrl
buildCraftQueryUrl
is the core function for building query URLs. It takes an elementType
as an argument and allows you to chain various methods to specify query parameters. This function makes it easy to generate URLs to fetch specific data from Craft CMS.
Example Usage
import { buildCraftQueryUrl } from 'js-craftcms-api';
// Build URL for fetching a single address
const url = buildCraftQueryUrl('addresses').id(1).buildBaseUrl('one');
// Result: /v1/api/queryApi/customQuery?elementType=addresses&id=1&one=1
// Build URL for fetching a single asset
const url = buildCraftQueryUrl('assets').id(1).buildBaseUrl('one');
// Result: /v1/api/queryApi/customQuery?elementType=assets&id=1&one=1
// Build URL for fetching a single entry
const url = buildCraftQueryUrl('entries').id(1).buildBaseUrl('one');
// Result: /v1/api/queryApi/customQuery?elementType=entries&id=1&one=1
// Build URL for fetching a single user
const url = buildCraftQueryUrl('users').id(1).buildBaseUrl('one');
// Result: /v1/api/queryApi/customQuery?elementType=users&id=1&one=1
Note
For a full list of available methods, refer to the API documentation.
You can use the generated URL to make a fetch request to your Craft CMS backend.
Preview Mode
buildCraftQueryUrl()
automatically handles preview mode by injecting the necessary token into the URL. This feature ensures that you can easily preview content without additional setup.