Instead of specifying the API token as a parameter, you can pass it as an environment variable:
$ export DATO_API_TOKEN=abc123$ ./node_modules/.bin/dato dump
The CLI tool also loads environment variables from a .env
file, so you can place the token there and forget about it (just make sure not to publish your .env
file on Github):
$ echo '.env' >> .gitignore$ echo 'DATO_API_TOKEN=abc123' >> .env$ ./node_modules/.bin/dato dump
If you are working on development/staging environment, you might want to preview the latest version of records instead of the published one. In this case, you can add a --preview
flag:
$ ./node_modules/.bin/dato dump --preview
To explicitely read data from a specific environment and not from the primary one you can use the --environment
flag:
$ ./node_modules/.bin/dato dump --environment=my-sandbox-environment
The dump
command will read a file dato.config.js
(or the file passed by the --config
option). This file should export a function that instructs how to transform the content stored remotely on DatoCMS into local files.
Let's watch a simple example to get started:
// dato.config.jsmodule.exports = (dato, root, i18n) => {const content = { hello: "world" }root.createDataFile("data/foobar.yml", 'yaml', content)}
Now run dato dump
:
$ ./node_modules/.bin/dato dump✔ Fetching content from DatoCMS...* Written data/foobar.yml
Great! In this example, root.createDataFile()
is a method made available to you that can generate YAML/TOML/JSON files. It's perfect to generate Hugo data files.
You can also generate Hugo posts and types with the root.createPost()
method:
// dato.config.jsmodule.exports = (dato, root, i18n) => {root.createPost("content/post/my-post.md", "yaml", {frontmatter: {title: "First article",type: "post",categories: ["random"],weight: 4,date: "2012-04-06",},content: "Lorem **ipsum dolor sit amet**, consectetur adipiscing elit."});}
If you need to place a collection of posts within a folder, you can use the root.directory
method, so that every time the dump
command is executed, previous content of the directory will be erased:
// dato.config.jsmodule.exports = (dato, root, i18n) => {root.directory("content/post", (dir) => {for (let i = 0; i < 10; i++) {dir.createPost(`post-${i}.md`, "yaml", {frontmatter: {title: `Article ${i}`,type: "post",categories: ["random"],weight: 4,date: "2012-04-06",},content: "Lorem **ipsum dolor sit amet**, consectetur adipiscing elit."});}});}
Now that you know how you can create local files, the final step is to start generating them with data coming from DatoCMS. An object called dato
is available exactly for this purpose:
// dato.config.jsmodule.exports = (dato, root, i18n) => {// inside a "post" directory...root.directory("content/post", (dir) => {// ...iterate over the "Blog post" records...dato.blogPosts.forEach((blogPost) => {// ...and create a markdown file for each article!dir.createPost(`${blogPost.slug}.md`, "yaml", {frontmatter: {title: blogPost.title,type: "post",categories: blogPost.categories.map(cat => cat.slug),date: blogPost.publishedAt,},content: blogPost.content});}});}
Once your dato.config.js
is ready, just run the dato dump
command: you should see your Hugo project populated with content. Run hugo -w
and enjoy!
Obviously, that's just a quick tour: you can learn all the details about how to access your records inside your config file in the following sections.
If your dump is failing because of timeouts, you can try customizing the pageSize
parameter of dato dump
. By default we fetch the maximum number of records in one go, which is 500. But you can try fetching less and see if it solves the issue. If it doesn't work please reach out at our support.