### Create a search index request examples Source: https://www.datocms.com/docs/content-management-api/resources/search-index/create?language=http Examples of how to perform a POST request to create a search index using different methods. ```HTTP POST https://site-api.datocms.com/search-indexes HTTP/1.1 Authorization: Bearer YOUR-API-TOKEN Accept: application/json X-Api-Version: 3 Content-Type: application/vnd.api+json { "data": { "type": "search_index", "attributes": { "name": "Production Website", "enabled": true, "frontend_url": "https://www.mywebsite.com/" } } } ``` ```bash curl -g 'https://site-api.datocms.com/search-indexes' \ -X POST \ -H "Authorization: Bearer YOUR-API-TOKEN" \ -H "Accept: application/json" \ -H "X-Api-Version: 3" \ -H "Content-Type: application/vnd.api+json" \ --data-binary '{"data":{"type":"search_index","attributes":{"name":"Production Website","enabled":true,"frontend_url":"https://www.mywebsite.com/"}}}' ``` ```javascript await fetch("https://site-api.datocms.com/search-indexes", { method: "POST", headers: { Authorization: "Bearer YOUR-API-TOKEN", Accept: "application/json", "X-Api-Version": "3", "Content-Type": "application/vnd.api+json", }, body: JSON.stringify({ data: { type: "search_index", attributes: { name: "Production Website", enabled: true, frontend_url: "https://www.mywebsite.com/", }, }, }), }); ``` -------------------------------- ### Upload Progress Output Example Source: https://www.datocms.com/docs/content-management-api/resources/upload/create.md This is an example of the console output you might see when monitoring upload progress. ```javascript DOWNLOADING_FILE { url: "https://example.com/image.png", progress: 20 } DOWNLOADING_FILE { url: "https://example.com/image.png", progress: 90 } DOWNLOADING_FILE { url: "https://example.com/image.png", progress: 100 } REQUESTING_UPLOAD_URL { filaname: 'image.png' } UPLOADING_FILE { progress: 10 } UPLOADING_FILE { progress: 80 } UPLOADING_FILE { progress: 100 } CREATING_UPLOAD_OBJECT undefined ``` -------------------------------- ### Returned Output Example Source: https://www.datocms.com/docs/content-management-api/resources/upload-tag/create.md This is an example of the JSON output returned after successfully creating an upload tag. ```json { id: "42", name: "Pictures of me" } ``` -------------------------------- ### Retrieve SSO Settings Requests Source: https://www.datocms.com/docs/content-management-api/resources/sso-settings/self?language=http Examples of how to perform a GET request to retrieve SSO settings using different methods. ```HTTP GET https://site-api.datocms.com/sso-settings HTTP/1.1 Authorization: Bearer YOUR-API-TOKEN Accept: application/json X-Api-Version: 3 ``` ```bash curl -g 'https://site-api.datocms.com/sso-settings' \ \ -H "Authorization: Bearer YOUR-API-TOKEN" \ -H "Accept: application/json" \ -H "X-Api-Version: 3" ``` ```javascript await fetch("https://site-api.datocms.com/sso-settings", { headers: { Authorization: "Bearer YOUR-API-TOKEN", Accept: "application/json", "X-Api-Version": "3", }, }); ``` -------------------------------- ### Example Menu Item Output Source: https://www.datocms.com/docs/content-management-api/resources/menu-item/self.md This is an example of the JSON structure returned when successfully retrieving a menu item. ```json { "id": "uinr2zfqQLeCo_1O0-ao-Q", "label": "Posts", "position": 1, "external_url": "", "open_in_new_tab": true, "item_type": { "type": "item_type", "id": "DxMaW10UQiCmZcuuA-IkkA" }, "item_type_filter": { "type": "item_type_filter", "id": "FF-P5of6Qp-DD2w0xoaa6Q" }, "parent": null, "children": [{ "type": "menu_item", "id": "uinr2zfqQLeCo_1O0-ao-Q" }] } ``` -------------------------------- ### Subscription Feature Output Example Source: https://www.datocms.com/docs/content-management-api/resources/subscription-feature/instances.md This is an example of the resource object format returned for a subscription feature. ```javascript { id: "locales", code: "sso", enabled: true } ``` -------------------------------- ### Role resource object example Source: https://www.datocms.com/docs/content-management-api/resources/role/instances.md This is an example of the JSON output when listing roles, detailing permissions and configurations for a specific role. ```json { "id": "34", "name": "Editor", "can_edit_site": true, "can_edit_favicon": true, "can_edit_schema": true, "can_manage_menu": true, "can_manage_users": true, "can_manage_shared_filters": true, "can_manage_search_indexes": true, "can_manage_upload_collections": true, "can_manage_environments": true, "can_manage_webhooks": true, "environments_access": "primary_only", "can_manage_sso": true, "can_access_audit_log": true, "can_manage_workflows": true, "can_edit_environment": true, "can_promote_environments": true, "can_manage_build_triggers": true, "can_manage_access_tokens": true, "can_perform_site_search": true, "can_access_build_events_log": true, "can_access_search_index_events_log": true, "positive_item_type_permissions": [{ "environment": "main", "action": "all" }], "negative_item_type_permissions": [{ "environment": "main", "action": "all" }], "positive_upload_permissions": [{ "action": "all", "environment": "main" }], "negative_upload_permissions": [{ "action": "all", "environment": "main" }], "positive_build_trigger_permissions": [{}], "negative_build_trigger_permissions": [{}], "positive_search_index_permissions": [{}], "negative_search_index_permissions": [{}], "meta": { "final_permissions": { "can_edit_site": true, "can_edit_favicon": true, "can_edit_schema": true, "can_manage_menu": true, "can_manage_users": true, "can_manage_environments": true, "can_manage_webhooks": true, "environments_access": "primary_only", "can_manage_sso": true, "can_access_audit_log": true, "can_manage_workflows": true, "can_edit_environment": true, "can_promote_environments": true, "can_manage_shared_filters": true, "can_manage_search_indexes": true, "can_manage_build_triggers": true, "can_manage_upload_collections": true, "can_manage_access_tokens": true, "can_perform_site_search": true, "can_access_build_events_log": true, "can_access_search_index_events_log": true, "positive_item_type_permissions": [{ "environment": "main", "action": "all" }], "negative_item_type_permissions": [{ "environment": "main", "action": "all" }], "positive_upload_permissions": [{ "action": "all", "environment": "main" }], "negative_upload_permissions": [{ "action": "all", "environment": "main" }], "positive_build_trigger_permissions": [{}], "negative_build_trigger_permissions": [{}], "positive_search_index_permissions": [{}], "negative_search_index_permissions": [{}], } }, "inherits_permissions_from": [{ "type": "role", "id": "34" }] } ``` -------------------------------- ### List Referenced Records Examples Source: https://www.datocms.com/docs/content-management-api/resources/item/references?language=http Examples showing how to request records that link to a specific item ID using various methods. ```HTTP 1 GET https://site-api.datocms.com/items/:item_id/references HTTP/1.1 2 Authorization: Bearer YOUR-API-TOKEN 3 Accept: application/json 4 X-Api-Version: 3 ``` ```bash 1 curl -g 'https://site-api.datocms.com/items/:item_id/references' \ 2 \ 3 -H "Authorization: Bearer YOUR-API-TOKEN" \ 4 -H "Accept: application/json" \ 5 -H "X-Api-Version: 3" ``` ```javascript 1 await fetch("https://site-api.datocms.com/items/:item_id/references", { 2 headers: { 3 Authorization: "Bearer YOUR-API-TOKEN", 4 Accept: "application/json", 5 "X-Api-Version": "3", 6 }, 7 }); ``` -------------------------------- ### Install DatoCMS CMA Client Packages Source: https://www.datocms.com/docs/content-management-api/using-the-nodejs-clients.md Install the appropriate DatoCMS CMA client package for your environment. The generic client is recommended for most cases. ```bash npm install @datocms/cma-client # Generic/agnostic (recommended for most cases) npm install @datocms/cma-client-node # Node.js with filesystem access npm install @datocms/cma-client-browser # Browser-optimized ``` -------------------------------- ### JavaScript Client Example for Upload Request Source: https://www.datocms.com/docs/content-management-api/resources/upload-request/create.md Example demonstrating how to use the DatoCMS JavaScript client to create an upload request. ```javascript import { buildClient } from "@datocms/cma-client-node"; async function run() { const client = buildClient({ apiToken: process.env.DATOCMS_API_TOKEN }); const uploadRequest = await client.uploadRequest.create({}); console.log(uploadRequest); } run(); ``` -------------------------------- ### Deploy Activity Object Payload Example Source: https://www.datocms.com/docs/content-management-api/resources/build-event.md This example shows the structure of the 'data' object within a deploy activity payload, containing details about the build request and response. ```json { "request_body": "{\"object_kind\":\"build\",\"ref\":\"master\",\"tag\":false,\"before_sha\":\"0000000000000000000000000000000000000000\",\"sha\":\"ecfccf5ea28af900c14b499a2b762e029c7492\",\"build_id\":10495,\"build_name\":\"build\",\"build_stage\":\"test\",\"build_status\":\"success\",\"build_started_at\":\"2016-09-20 18:49:22 UTC\",\"build_finished_at\":\"2016-09-20 18:50:24 UTC\",\"build_duration\":62.279854524,\"build_allow_failure\":false,\"project_id\":195,\"project_id\":\"Stefano Verna / awesome-website\",\"user\":{\"id\":null,\"name\":null,\"email\":null\},\"commit\":{\"id\":6754,\"sha\":\"ecfccf5ea28af900c6614b499a2b762e029c7492\",\"message\":\"Update gems\\n\",\"author_name\":\"Stefano Verna\",\"author_email\":\"s.verna@datocms.com\",\"status\":\"success\",\"duration\":62,\"started_at\":\"2016-09-20 18:49:22 UTC\",\"finished_at\":\"2016-09-20 18:50:24 UTC\"},\"repository\":{\"name\":\"awesome-website\",\"url\":\"git@gitlab.com:stefanoverna/awesome-website.git\",\"description\":\"\",\"visibility_level\":0}}", "request_headers": { "Via": "1.1 vegur", "Host": "webhooks.datocms.com", "Origin": null, "Version": "HTTP/1.1", "Connection": "close", "Connect-Time": "0", "X-Request-Id": "5c1beced-0fe3-4c5b-b45d-68ba4a15b5f3", "X-Gitlab-Event": "Build Hook", "X-Forwarded-For": "46.101.135.219", "X-Request-Start": "1474397424903", "Total-Route-Time": "0", "X-Forwarded-Port": "443", "X-Forwarded-Proto": "https" } } ``` -------------------------------- ### GET Request for Roles (HTTP) Source: https://www.datocms.com/docs/content-management-api/resources/role/instances?language=http Example of an HTTP GET request to retrieve roles from the DatoCMS API. Ensure to include your API token and specify the API version. ```http GET https://site-api.datocms.com/roles HTTP/1.1 Authorization: Bearer YOUR-API-TOKEN Accept: application/json X-Api-Version: 3 ``` -------------------------------- ### List All Plugins - Fetch API Request Source: https://www.datocms.com/docs/content-management-api/resources/plugin/instances?language=http This JavaScript example shows how to fetch a list of all plugins using the Fetch API. It configures the request headers for authentication and API versioning. ```JavaScript await fetch("https://site-api.datocms.com/plugins", { headers: { Authorization: "Bearer YOUR-API-TOKEN", Accept: "application/json", "X-Api-Version": "3", }, }); ``` -------------------------------- ### Trigger a Deploy (fetch() Request) Source: https://www.datocms.com/docs/content-management-api/resources/build-trigger/trigger?language=http This JavaScript fetch example shows how to trigger a deploy. It includes the necessary headers for authentication and API versioning. ```JavaScript await fetch( "https://site-api.datocms.com/build-triggers/:build_trigger_id/trigger", { method: "POST", headers: { Authorization: "Bearer YOUR-API-TOKEN", Accept: "application/json", "X-Api-Version": "3", }, }, ); ``` -------------------------------- ### GET /items - Fetching Records with Pagination Source: https://www.datocms.com/docs/content-management-api/resources/item/instances?language=http This endpoint allows you to retrieve a list of records from DatoCMS. You can control the number of records returned and the starting position using `page[limit]` and `page[offset]` query parameters. Note that record counting starts from 0. ```APIDOC ## GET /items ### Description Retrieves a list of records with pagination support. ### Method GET ### Endpoint /items ### Query Parameters - **page[limit]** (integer) - Optional - The maximum number of records to return. - **page[offset]** (integer) - Optional - The number of records to skip from the beginning (0-indexed). ### Request Example ```http GET https://site-api.datocms.com/items?page[limit]=2&page[offset]=3 HTTP/1.1 Authorization: Bearer YOUR-API-TOKEN Accept: application/json X-Api-Version: 3 ``` ### Response #### Success Response (200) - **data** (array) - An array of item objects. - **meta** (object) - Contains metadata about the response, including `total_count`. #### Response Example ```json { "data": [ { "id": "VDd0Ig2WTZukQ8DU9ZskXg", "type": "item", "attributes": { "name": "Shout" }, "relationships": {}, "meta": { "created_at": "2024-03-12T20:08:33.887+00:00", "updated_at": "2024-03-12T20:08:33.895+00:00", "published_at": "2024-03-12T20:08:33.945+00:00", "status": "published" } } ], "meta": { "total_count": 264 } } ``` ``` ```APIDOC ## GET /items (cURL Example) ### Description Example cURL command to fetch records with pagination. ### Method GET ### Endpoint /items ### Query Parameters - **page[limit]** (integer) - Optional - The maximum number of records to return. - **page[offset]** (integer) - Optional - The number of records to skip from the beginning (0-indexed). ### Request Example ```bash curl -g 'https://site-api.datocms.com/items?page[limit]=2&page[offset]=3' \ -H "Authorization: Bearer YOUR-API-TOKEN" \ -H "Accept: application/json" \ -H "X-Api-Version: 3" ``` ``` ```APIDOC ## GET /items (fetch() Example) ### Description Example JavaScript `fetch` call to retrieve records with pagination. ### Method GET ### Endpoint /items ### Query Parameters - **page[limit]** (integer) - Optional - The maximum number of records to return. - **page[offset]** (integer) - Optional - The number of records to skip from the beginning (0-indexed). ### Request Example ```javascript await fetch("https://site-api.datocms.com/items?page[limit]=2&page[offset]=3", { headers: { Authorization: "Bearer YOUR-API-TOKEN", Accept: "application/json", "X-Api-Version": "3", }, }); ``` ``` -------------------------------- ### GET Request for Roles (JavaScript Fetch) Source: https://www.datocms.com/docs/content-management-api/resources/role/instances?language=http Example using the JavaScript fetch API to retrieve roles. This demonstrates how to set the required headers for the request. ```javascript await fetch("https://site-api.datocms.com/roles", { headers: { Authorization: "Bearer YOUR-API-TOKEN", Accept: "application/json", "X-Api-Version": "3", }, }); ``` -------------------------------- ### Create Menu Item - Basic Example Source: https://www.datocms.com/docs/content-management-api/resources/menu-item/create.md Use this snippet to create a new menu item. Ensure the DATOCMS_API_TOKEN environment variable is set. ```javascript import { buildClient } from "@datocms/cma-client-node"; async function run() { const client = buildClient({ apiToken: process.env.DATOCMS_API_TOKEN }); const menuItem = await client.menuItems.create({ label: "Posts" }); // Check the 'Returned output' tab for the result ☝️ console.log(menuItem); } run(); ``` ```javascript { id: "uinr2zfqQLeCo_1O0-ao-Q", label: "Posts", position: 1, external_url: "", open_in_new_tab: true, item_type: { type: "item_type", id: "DxMaW10UQiCmZcuuA-IkkA" }, item_type_filter: { type: "item_type_filter", id: "FF-P5of6Qp-DD2w0xoaa6Q" }, parent: null, children: [{ type: "menu_item", id: "uinr2zfqQLeCo_1O0-ao-Q" }], } ``` -------------------------------- ### Retrieve an upload request Source: https://www.datocms.com/docs/content-management-api/resources/upload/self?language=http Examples of how to perform a GET request to retrieve an upload resource. Replace :upload_id with the actual ID and YOUR-API-TOKEN with your valid API token. ```HTTP 1 GET https://site-api.datocms.com/uploads/:upload_id HTTP/1.1 2 Authorization: Bearer YOUR-API-TOKEN 3 Accept: application/json 4 X-Api-Version: 3 ``` ```bash 1 curl -g 'https://site-api.datocms.com/uploads/:upload_id' \ 2 \ 3 -H "Authorization: Bearer YOUR-API-TOKEN" \ 4 -H "Accept: application/json" \ 5 -H "X-Api-Version: 3" ``` ```javascript 1 await fetch("https://site-api.datocms.com/uploads/:upload_id", { 2 headers: { 3 Authorization: "Bearer YOUR-API-TOKEN", 4 Accept: "application/json", 5 "X-Api-Version": "3", 6 }, 7 }); ``` -------------------------------- ### Initializing the legacy JS client with an environment Source: https://www.datocms.com/docs/content-management-api/setting-the-environment.md Configure the legacy SiteClient by providing the environment name in the options object. ```javascript const { SiteClient } = require('datocms-client'); const client = new SiteClient('YOUR-API-TOKEN', { environment: 'my-sandbox-environment', }); ``` -------------------------------- ### GET Request for Roles (cURL) Source: https://www.datocms.com/docs/content-management-api/resources/role/instances?language=http Example using cURL to fetch roles from the DatoCMS API. This command includes necessary headers for authentication and content negotiation. ```curl curl -g 'https://site-api.datocms.com/roles' \ \ -H "Authorization: Bearer YOUR-API-TOKEN" \ -H "Accept: application/json" \ -H "X-Api-Version: 3" ``` -------------------------------- ### Create a Plugin Source: https://www.datocms.com/docs/content-management-api/resources/plugin/create?language=http Examples of how to send a POST request to create a new plugin in DatoCMS. ```http POST https://site-api.datocms.com/plugins HTTP/1.1 Authorization: Bearer YOUR-API-TOKEN Accept: application/json X-Api-Version: 3 Content-Type: application/vnd.api+json { "data": { "type": "plugin", "attributes": {} } } ``` ```bash curl -g 'https://site-api.datocms.com/plugins' \ -X POST \ -H "Authorization: Bearer YOUR-API-TOKEN" \ -H "Accept: application/json" \ -H "X-Api-Version: 3" \ -H "Content-Type: application/vnd.api+json" \ --data-binary '{"data":{"type":"plugin","attributes":{}}}' ``` ```javascript await fetch("https://site-api.datocms.com/plugins", { method: "POST", headers: { Authorization: "Bearer YOUR-API-TOKEN", Accept: "application/json", "X-Api-Version": "3", "Content-Type": "application/vnd.api+json", }, body: JSON.stringify({ data: { type: "plugin", attributes: {} } }), }); ``` -------------------------------- ### Initialize Client for Sandbox Environment Source: https://www.datocms.com/docs/content-management-api/using-the-nodejs-clients.md Initialize the client to target a specific sandbox environment by providing the environment name during client construction. ```javascript import { buildClient } from '@datocms/cma-client-node'; const client = buildClient({ apiToken: process.env.DATOCMS_API_TOKEN, environment: 'my-sandbox-environment', }); ``` -------------------------------- ### Create Build Trigger - JavaScript Source: https://www.datocms.com/docs/content-management-api/resources/access-token/destroy.md Example of creating a build trigger using the DatoCMS JavaScript SDK. Ensure you have the SDK installed and configured with your API token. ```javascript import { buildTrigger } from 'datocms-client'; (async () => { const trigger = await buildTrigger.create({ name: 'My new trigger', // ... other properties }); console.log(trigger); })(); ``` -------------------------------- ### JavaScript Fetch Request to Get Uploads Source: https://www.datocms.com/docs/content-management-api/resources/upload/instances?language=http An example using the Fetch API in JavaScript to request upload data from DatoCMS. Remember to replace YOUR-API-TOKEN with your actual token. ```JavaScript await fetch("https://site-api.datocms.com/uploads", { headers: { Authorization: "Bearer YOUR-API-TOKEN", Accept: "application/json", "X-Api-Version": "3", }, }); ``` -------------------------------- ### List All Plugins - CURL Request Source: https://www.datocms.com/docs/content-management-api/resources/plugin/instances?language=http This example demonstrates how to list all plugins using cURL. It includes the necessary headers for authorization, content type, and API version. ```Shell curl -g 'https://site-api.datocms.com/plugins' \ -H "Authorization: Bearer YOUR-API-TOKEN" \ -H "Accept: application/json" \ -H "X-Api-Version: 3" ``` -------------------------------- ### Retrieve filtered records via HTTP Source: https://www.datocms.com/docs/content-management-api/resources/item/instances?language=http Use the 'in' filter for the 'name' field by providing multiple separate parameters. This example demonstrates a GET request with multiple filter criteria. ```http 1 GET https://site-api.datocms.com/items?filter[type]=dog&filter[fields][name][in][]=Buddy&filter[fields][name][in][]=Rex&filter[fields][breed][eq]=mixed&filter[fields][_updated_at][gt]=2020-04-18T00:00:00&order_by=_updated_at_ASC HTTP/1.1 2 Authorization: Bearer YOUR-API-TOKEN 3 Accept: application/json 4 X-Api-Version: 3 ``` ```bash 1 curl -g 'https://site-api.datocms.com/items?filter[type]=dog&filter[fields][name][in][]=Buddy&filter[fields][name][in][]=Rex&filter[fields][breed][eq]=mixed&filter[fields][_updated_at][gt]=2020-04-18T00:00:00&order_by=_updated_at_ASC' \ 2 \ 3 -H "Authorization: Bearer YOUR-API-TOKEN" \ 4 -H "Accept: application/json" \ 5 -H "X-Api-Version: 3" ``` ```javascript 1 await fetch( 2 "https://site-api.datocms.com/items?filter[type]=dog&filter[fields][name][in][]=Buddy&filter[fields][name][in][]=Rex&filter[fields][breed][eq]=mixed&filter[fields][_updated_at][gt]=2020-04-18T00:00:00&order_by=_updated_at_ASC", 3 { 4 headers: { 5 Authorization: "Bearer YOUR-API-TOKEN", 6 Accept: "application/json", 7 "X-Api-Version": "3", 8 }, 9 }, 10 ); ``` -------------------------------- ### Retrieve site information with JavaScript Source: https://www.datocms.com/docs/content-management-api/resources/site/self.md Uses the buildClient function to initialize the client and fetch the site resource. Requires a valid DATOCMS_API_TOKEN environment variable. ```javascript import { buildClient } from "@datocms/cma-client-node"; async function run() { const client = buildClient({ apiToken: process.env.DATOCMS_API_TOKEN }); const site = await client.site.find(); // Check the 'Returned output' tab for the result ☝️ console.log(site); } run(); ``` ```json { id: "155", domain: "admin.my-awesome-website.com", favicon: "123", global_seo: {}, google_maps_api_token: "xxxxxxxxxxxxx", imgix_host: "www.datocms-assets.com", internal_domain: "my-website.admin.datocms.com", last_data_change_at: "2017-03-30T09:29:14.872Z", locales: ["en"], name: "My Awesome Website", no_index: true, require_2fa: false, theme: { type: "monochromatic", hue: 16, primary_color: { red: 128, green: 128, blue: 128, alpha: 128 }, light_color: { red: 128, green: 128, blue: 128, alpha: 128 }, accent_color: { red: 128, green: 128, blue: 128, alpha: 128 }, dark_color: { red: 128, green: 128, blue: 128, alpha: 128 }, logo: "123", }, timezone: "Europe/London", ip_tracking_enabled: true, force_use_of_sandbox_environments: true, assets_cdn_default_settings: { image: {}, video: {} }, meta: { created_at: "2020-04-21T07:57:11.124Z", improved_timezone_management: true, improved_hex_management: true, improved_gql_multilocale_fields: true, improved_gql_visibility_control: true, improved_boolean_fields: true, draft_mode_default: true, improved_validation_at_publishing: true, improved_exposure_of_inline_blocks_in_cda: true, improved_items_listing: true, milliseconds_in_datetime: true, }, item_types: [{ type: "item_type", id: "DxMaW10UQiCmZcuuA-IkkA" }], owner: { type: "account", id: "312" }, } ``` -------------------------------- ### Retrieve a Job Result by ID (JavaScript Fetch API) Source: https://www.datocms.com/docs/content-management-api/resources/field/update?language=http An example using the JavaScript Fetch API to get a job result. This code snippet shows how to set up the request headers, including the API token. ```javascript await fetch("https://site-api.datocms.com/job-results/:job_result_id", { headers: { Authorization: "Bearer YOUR-API-TOKEN", Accept: "application/json", "X-Api-Version": "3", }, }); ``` -------------------------------- ### Get Single Subscription Limit (fetch() Request) Source: https://www.datocms.com/docs/content-management-api/resources/subscription-limit/self?language=http Illustrates fetching a single subscription limit using the JavaScript fetch API. This example includes the required headers for authorization and API version. Remember to substitute placeholder values. ```JavaScript await fetch( "https://site-api.datocms.com/subscription-limits/:subscription_limit_id", { headers: { Authorization: "Bearer YOUR-API-TOKEN", Accept: "application/json", "X-Api-Version": "3", }, }, ); ``` -------------------------------- ### List all record versions (fetch() Request) Source: https://www.datocms.com/docs/content-management-api/resources/item-version/instances?language=http This Javascript example shows how to fetch all versions of an item using the `fetch` API. It includes the required headers for authentication and API versioning. ```Javascript await fetch("https://site-api.datocms.com/items/:item_id/versions", { headers: { Authorization: "Bearer YOUR-API-TOKEN", Accept: "application/json", "X-Api-Version": "3", }, }); ``` -------------------------------- ### Create a new webhook with DatoCMS API Source: https://www.datocms.com/docs/content-management-api/resources/webhook/create.md Use this snippet to create a new webhook. Ensure you have the `@datocms/cma-client-node` package installed and your API token set in the environment variable `DATOCMS_API_TOKEN`. This example configures a webhook to trigger on item updates. ```javascript import { buildClient } from "@datocms/cma-client-node"; async function run() { const client = buildClient({ apiToken: process.env.DATOCMS_API_TOKEN }); const webhook = await client.webhooks.create({ name: "Item type creation/update", url: "https://www.example.com/webhook", headers: { "X-Foo": "Bar" }, events: [{ entity_type: "item", event_types: ["update"] }], custom_payload: '{ "message": "{{event_type}} event triggered on {{entity_type}}!", "entity_id": "{{#entity}}{{id}}{{/entity}}"] }', http_basic_user: "user", http_basic_password: "password", }); // Check the 'Returned output' tab for the result ☝️ console.log(webhook); } run(); ``` -------------------------------- ### List all environments (fetch() Request) Source: https://www.datocms.com/docs/content-management-api/resources/environment/instances?language=http An example using the fetch API to list environments from the DatoCMS Content Management API. This code snippet shows how to set up the request headers, including the API token and version. ```javascript await fetch("https://site-api.datocms.com/environments", { headers: { Authorization: "Bearer YOUR-API-TOKEN", Accept: "application/json", "X-Api-Version": "3", }, }); ``` -------------------------------- ### Create Deploy Activity - GraphQL Source: https://www.datocms.com/docs/content-management-api/resources/access-token/destroy.md Example of creating a deploy activity using GraphQL. This is used to manually trigger a deployment. ```graphql mutation CreateDeployActivity($filter: DeployActivityFilterInput, $build_trigger_id: ItemId) { createDeployActivity(filter: $filter, build_trigger_id: $build_trigger_id) { id created_at } } ``` -------------------------------- ### Filter columns definition example Source: https://www.datocms.com/docs/content-management-api/resources/item-type-filter/create?language=http Example array defining the columns to display within a filter. ```json [ { name: "_preview", width: 0.6 }, { name: "slug", width: 0.1 }, { name: "_status", width: 0.1 }, { name: "_updated_at", width: 0.2 }, ] ``` -------------------------------- ### Initialize Client with Basic Logging Source: https://www.datocms.com/docs/content-management-api/using-the-nodejs-clients.md Initialize the client with basic logging enabled to view HTTP requests and responses. Other logging levels are available for more detailed debugging. ```javascript import { buildClient, LogLevel } from '@datocms/cma-client-node'; const client = buildClient({ apiToken: process.env.DATOCMS_API_TOKEN, logLevel: LogLevel.BASIC, }); ``` -------------------------------- ### Optimistic locking version example Source: https://www.datocms.com/docs/content-management-api/resources/item/update?language=http Example value for the meta.current_version field used in optimistic locking. ```json "4234" ``` -------------------------------- ### Example Upload Filter Object Source: https://www.datocms.com/docs/content-management-api/resources/upload-filter/instances.md This is an example of the structure of an upload filter resource object returned by the API. ```json { "id": "-Lo34LFSTLmgPToamzJLcg", "name": "Draft posts", "filter": { "status": { "eq": "draft" } }, "shared": true, } ``` -------------------------------- ### List all record versions (CURL Request) Source: https://www.datocms.com/docs/content-management-api/resources/item-version/instances?language=http This example demonstrates how to list all versions of an item using CURL. It includes necessary headers for authentication and content type. ```Shell curl -g 'https://site-api.datocms.com/items/:item_id/versions' \ -H "Authorization: Bearer YOUR-API-TOKEN" \ -H "Accept: application/json" \ -H "X-Api-Version: 3" ``` -------------------------------- ### Asset Update Output Example Source: https://www.datocms.com/docs/content-management-api/resources/upload/update.md Example JSON structure returned after performing an asset update operation. ```javascript { id: 'FWYwxZZ1SUG1ReKlzdDAEA', author: 'New author!', copyright: 'New copyright', default_field_metadata: { en: { alt: 'new default alt', title: 'new default title', focal_point: [Object], custom_data: {} } } } ``` -------------------------------- ### Fetch Site Information with Accept Header Source: https://www.datocms.com/docs/content-management-api.md Use this command to fetch site information, ensuring the 'Accept: application/json' and 'X-Api-Version: 3' headers are included. ```bash curl \ -H 'Accept: application/json' \ -H 'X-Api-Version: 3' \ https://site-api.datocms.com/site ``` -------------------------------- ### Create a Webhook via cURL Source: https://www.datocms.com/docs/content-management-api/resources/webhook/create?language=http Command-line example for creating a webhook using the cURL utility. ```bash curl -g 'https://site-api.datocms.com/webhooks' \ -X POST \ -H "Authorization: Bearer YOUR-API-TOKEN" \ -H "Accept: application/json" \ -H "X-Api-Version: 3" \ -H "Content-Type: application/vnd.api+json" \ --data-binary '{"data":{"type":"webhook","attributes":{"name":"Item type creation/update","url":"https://www.example.com/webhook","headers":{"X-Foo":"Bar"},"events":[{"entity_type":"item","event_types":["update"]}],"custom_payload":"{ \"message\": \"{{event_type}} event triggered on {{entity_type}}!\", \"entity_id\": \"{{#entity}}{{id}}{{/entity}}\"] }","http_basic_user":"user","http_basic_password":"password"}}}' ``` -------------------------------- ### Retrieve an Environment (fetch() Request) Source: https://www.datocms.com/docs/content-management-api/resources/environment/self?language=http An example using the fetch API to retrieve environment details. Make sure to include the necessary headers for authentication and API versioning. ```JavaScript await fetch("https://site-api.datocms.com/environments/:environment_id", { headers: { Authorization: "Bearer YOUR-API-TOKEN", Accept: "application/json", "X-Api-Version": "3", }, }); ``` -------------------------------- ### Filter attributes example Source: https://www.datocms.com/docs/content-management-api/resources/item-type-filter/create?language=http Example structure for the attributes.filter object, following the format of the List all records endpoint. ```json { query: "foo bar", fields: { _status: { eq: "draft" }, title: { matches: { pattern: "qux", case_sensitive: "false", regexp: "false" }, }, }, } ``` -------------------------------- ### List all environments (HTTP Response) Source: https://www.datocms.com/docs/content-management-api/resources/environment/instances?language=http This is an example of a successful response when listing environments. It returns an array of environment objects, each with its ID, status, and metadata. ```json HTTP/1.1 200 OK Content-Type: application/json Cache-Control: cache-control: max-age=0, private, must-revalidate X-RateLimit-Limit: 30 X-RateLimit-Remaining: 28 { "data": [ { "type": "environment", "id": "main", "meta": { "status": "ready", "created_at": "2020-04-21T07:57:11.124Z", "read_only_mode": true, "last_data_change_at": "2020-04-21T07:57:11.124Z", "primary": true, "forked_from": "main" } } ] } ``` -------------------------------- ### Webhook Body Parameters Examples Source: https://www.datocms.com/docs/content-management-api/resources/webhook/create?language=http Examples of specific field values required for the webhook creation request body. ```json "Item type creation/update" ``` ```json "https://www.example.com/webhook" ``` ```json { "X-Foo": "Bar" } ``` ```json "item" ``` ```json '{ "message": "{{event_type}} event triggered on {{entity_type}}!", "entity_id": "{{#entity}}{{id}}{{/entity}}] }' ``` ```json "user" ``` ```json "password" ``` ```json "3" ``` -------------------------------- ### Create Deploy Activity - JavaScript Source: https://www.datocms.com/docs/content-management-api/resources/access-token/destroy.md Example of creating a deploy activity using the DatoCMS JavaScript SDK. This action initiates a new deployment. ```javascript import { deployActivity } from 'datocms-client'; (async () => { const activity = await deployActivity.create({ build_trigger_id: '12345', // ... other properties }); console.log(activity); })(); ``` -------------------------------- ### Update asset attributes example Source: https://www.datocms.com/docs/content-management-api/resources/upload/update?language=http Examples demonstrating how to modify asset properties using different request methods. ```HTTP PATCH /uploads/12345 HTTP/1.1 Host: site-api.datocms.com Authorization: Bearer YOUR_API_TOKEN Content-Type: application/vnd.api+json Accept: application/vnd.api+json { "data": { "type": "upload", "id": "12345", "attributes": { "basename": "new-filename" } } } ``` ```bash curl -X PATCH https://site-api.datocms.com/uploads/12345 \ -H "Authorization: Bearer YOUR_API_TOKEN" \ -H "Content-Type: application/vnd.api+json" \ -H "Accept: application/vnd.api+json" \ -d '{ "data": { "type": "upload", "id": "12345", "attributes": { "basename": "new-filename" } } }' ``` ```javascript fetch('https://site-api.datocms.com/uploads/12345', { method: 'PATCH', headers: { 'Authorization': 'Bearer YOUR_API_TOKEN', 'Content-Type': 'application/vnd.api+json', 'Accept': 'application/vnd.api+json' }, body: JSON.stringify({ data: { type: 'upload', id: '12345', attributes: { basename: 'new-filename' } } }) }); ``` -------------------------------- ### SSO User Resource Object Example Source: https://www.datocms.com/docs/content-management-api/resources/sso-user/copy_users.md This is an example of the structure of an sso_user resource object returned by the copyUsers method. ```javascript { id: "312", username: "mark.smith@example.com", external_id: "Ja23ekjhsad", is_active: true, first_name: "Mark", last_name: "Smith", meta: { last_access: "2018-03-25T21:50:24.914Z" }, groups: [{ type: "sso_group", id: "312" }], role: { type: "role", id: "34" }, } ``` -------------------------------- ### Fetch Items using cURL Source: https://www.datocms.com/docs/content-management-api/resources/item/instances?language=http A command-line example using cURL to fetch items. This is useful for scripting or quick testing. Replace YOUR-API-TOKEN with your actual token. ```curl curl -g 'https://site-api.datocms.com/items' \ \ -H "Authorization: Bearer YOUR-API-TOKEN" \ -H "Accept: application/json" \ -H "X-Api-Version: 3" ``` -------------------------------- ### Filter Object Property Examples Source: https://www.datocms.com/docs/content-management-api/resources/item-type-filter.md Examples of individual property values used within a model filter object. ```json "FF-P5of6Qp-DD2w0xoaa6Q" ``` ```json "Draft posts" ``` ```json { query: "foo bar", fields: { _status: { eq: "draft" }, title: { matches: { pattern: "qux", case_sensitive: "false", regexp: "false" }, }, }, } ``` ```json [ { name: "_preview", width: 0.6 }, { name: "slug", width: 0.1 }, { name: "_status", width: 0.1 }, { name: "_updated_at", width: 0.2 }, ] ``` ```json "_updated_at_ASC" ``` -------------------------------- ### Record ID Example Source: https://www.datocms.com/docs/content-management-api/resources/item/create?language=http Example of an RFC 4122 UUID in URL-safe base64 format used for record identification. ```json "hWl-mnkWRYmMCSTq4z_piQ" ``` -------------------------------- ### Create Plugin Fetch Request Source: https://www.datocms.com/docs/content-management-api/resources/plugin/create?language=http Example using the fetch API in Javascript to create a new plugin. This shows how to construct the request with headers and body. ```javascript fetch("https://yourdomain.com/content/api/v1.0/plugins", { method: "POST", headers: { "Content-Type": "application/json", "Authorization": "Bearer YOUR_API_TOKEN" }, body: JSON.stringify({ "data": { "type": "plugin", "attributes": { "package_name": "datocms-plugin-star-rating-editor" } } }) }) .then(response => response.json()) .then(data => console.log(data)); ```