Javascript
In this section, we will take you on a jouhrney through the powerful features and functionalities of our Javascript SDK, designed to empower you in creating seamless and innovative digital project that connect to Atlas CMS.
Source Code
How to install the client
In order to get started with the Atlas CMS JS SDJ you'll need only to install the package from the npm registry
npm install atlascms-js
Initialize the Client
import * as Atlas from 'atlascms-js'
//settings
const options = {
apiToken: "<api-token>",
projectId: "<project>"
};
//create client
const client = Atlas.createClient(options);
To initialize the client you need and Api Token and a Project Id you want to connect.
To get these information please check the documentation here and here.
Use the client
The client expose all methods we covered in the API Reference section and they are available in each service inside the client:
client.contents = for contents
client.models = for models and components
client.mediaLibrary = for assets and folders
client.users = for project's users and roles
For example we we want to fetch the Contents of the posts
collection we can use the client like this:
const myPosts = await client.contents.getContents("posts");
or if for example we want to fetch a media object we can use the client as the following:
const myMedia = await client.mediaLibrary.getAsset("64f995d2591a78b4a00c3a4f");
Query Builder
To simplify the creation of the query parameters you can use to filter Contents, the SDK implemente a fluent query builder that you can use to avoid string concatenation or many other logics in your code.
For example if you want to fetch the last 10 posts
where the category
field is equals
to 'sport' and the post date
is greater or equals
to 'January 1st 2023' ordered
descending by the date field
const filters = Atlas.createContentFilter()
.pageSize(10)
.eq("category","sport")
.gte("date","2023-01-01T00:00:00Z")
.sort("date", true)
.build();
const myPosts = await client.contents.getContents("posts", filters);
Last updated