microCMS JavaScript SDK
It helps you to use microCMS from JavaScript and Node.js applications.
![Discord](https://img.shields.io/badge/Discord-%235865F2.svg?style=for-the-badge&logo=discord&logoColor=white)
Getting Started
Install
Install npm package.
$ npm install microcms-js-sdk
or
$ yarn add microcms-js-sdk
CDN support.
<script src="https://unpkg.com/microcms-js-sdk@latest/dist/umd/microcms-js-sdk.js"></script>
How to use
First, create a client.
const { createClient } = require("microcms-js-sdk");
import { createClient } from 'microcms-js-sdk';
const client = createClient({
serviceDomain: "YOUR_DOMAIN",
apiKey: "YOUR_API_KEY",
});
When using with a browser.
<script>
const { createClient } = microcms;
const client = createClient({
serviceDomain: "YOUR_DOMAIN",
apiKey: "YOUR_API_KEY",
});
</script>
After, How to use get
it below.
client
.get({
endpoint: 'endpoint',
queries: { limit: 20, filters: 'createdAt[greater_than]2021' },
})
.then((res) => console.log(res))
.catch((err) => console.error(err));
client
.get({
endpoint: 'endpoint',
contentId: 'contentId',
queries: { fields: 'title,publishedAt' },
})
.then((res) => console.log(res))
.catch((err) => console.error(err));
And, Api corresponding to each content are also available. example.
client
.getList({
endpoint: 'endpoint',
})
.then((res) => console.log(res))
.catch((err) => console.error(err));
client
.getListDetail({
endpoint: 'endpoint',
contentId: 'contentId',
})
.then((res) => console.log(res))
.catch((err) => console.error(err));
client
.getObject({
endpoint: 'endpoint',
})
.then((res) => console.log(res))
.catch((err) => console.error(err));
CREATE API
The following is how to use the write system when making a request to the write system API.
client
.create({
endpoint: 'endpoint',
content: {
title: 'title',
body: 'body',
},
})
.then((res) => console.log(res.id))
.catch((err) => console.error(err));
client
.create({
endpoint: 'endpoint',
contentId: 'contentId',
content: {
title: 'title',
body: 'body',
},
})
.then((res) => console.log(res.id))
.catch((err) => console.error(err));
client
.create({
endpoint: 'endpoint',
content: {
title: 'title',
body: 'body',
},
isDraft: true,
})
.then((res) => console.log(res.id))
.catch((err) => console.error(err));
client
.create({
endpoint: 'endpoint',
contentId: 'contentId',
content: {
title: 'title',
body: 'body',
},
isDraft: true,
})
.then((res) => console.log(res.id))
.catch((err) => console.error(err));
UPDATE API
client
.update({
endpoint: 'endpoint',
contentId: 'contentId',
content: {
title: 'title',
},
})
.then((res) => console.log(res.id))
.catch((err) => console.error(err));
client
.update({
endpoint: 'endpoint',
content: {
title: 'title',
},
})
.then((res) => console.log(res.id))
.catch((err) => console.error(err));
DELETE API
client
.delete({
endpoint: 'endpoint',
contentId: 'contentId',
})
.catch((err) => console.error(err));
TypeScript
If you are using TypeScript, use getList
, getListDetail
, getObject
. This internally contains a common type of content.
type Content = {
text: string,
}
client.getList<Content>({
client.getListDetail<Content>({
client.getObject<Content>({
Write functions can also be performed type-safely.
type Content = {
title: string
body?: string
}
client.create<Content>({
endpoint: 'endpoint',
content: {
title: 'title',
body: 'body',
},
})
client.update<Content>({
endpoint: 'endpoint',
content: {
body: 'body',
},
})
Tips
Separate API keys for read and write
const readClient = createClient({
serviceDomain: 'serviceDomain',
apiKey: 'readApiKey',
})
const writeClient = createClient({
serviceDomain: 'serviceDomain',
apiKey: 'writeApiKey',
})
LICENSE
Apache-2.0