microCMS
Documentation
  • Pricing
  • Blog
  • FAQ
  • Contact
  • Sign In
  • Get started for free
Navigation
Tutorial
Next.js
  • Integration with Next.js
  • Getting Started with App Router
  • Tutorial (App Router)
  • Getting Started with Pages Router
  • Tutorial (Pages Router)
Astro
  • Getting Started
  • Integration with Astro
  • Tutorial
Nuxt
  • Integration with Nuxt
  • Getting Started
  • Tutorial
Gatsby
  • Integration with Gatsby
  • Getting Started
  • Tutorial
JavaScript
  • Integration with JavaScript
  • Getting Started (Browser)
  • Getting Started (Server)
PHP
  • Integration with PHP
  • Getting Started
Ruby
  • Integration with Ruby
  • Getting Started
Go
  • Integration with Go
  • Getting Started with microCMS
Manual
  • Introduction
Account & Login
  • Account Registration
  • Login
  • Email Address Authentication
Services
  • Creating a Service
  • Service Configuration
  • Template
  • Service Deletion
Payment
  • Change of Pricing Plan
  • Registering and Changing Payment Methods
API
  • Creating and Managing APIs
  • API Settings
  • Export/Import of API Schema
Fields
  • Text Field
  • Text Area
  • Image
  • Multiple Images
  • Date
  • Boolean
  • Selectable Field
  • Content Reference
  • Multiple Content References
  • Number
  • File
  • Extended Field
  • Repetition
  • Custom
Contents
  • CSV Export of Content
  • Content Creation
  • Content Editing
  • Content Management
  • View
  • Content Status
  • Custom Status
  • Setting the Content ID
  • Automatically Assigned Values for Content
  • CSV Import of Content
  • API Preview
  • Examples of Using Query Parameters with API Preview
  • Integration Methods by Rendering Type
  • Screen Preview
  • Setting Up Webhooks for Content
Rich Editor
  • How to Use the Rich Editor
  • Rich Editor WRITE API
  • How to Use the Old Rich Editor
Reviews
  • Review
  • AI Review
Media
  • Media Management
  • Changing Media Filters and Display Order
  • Media Tag Configuration
  • Setting Up Media Webhooks
  • Custom Domain Settings for Media
Permission Management
  • Member Management
  • Roles (Permission Management in the Admin Panel)
Environments
  • Multiple Environments (Development Environment)
  • Merging API Settings Between Environments
Connection
  • Amazon S3 Integration
Logs
  • Audit Log
  • Data Transfer Volume
Security
  • Content Update History
  • IP Restrictions for Management Dashboard
  • IP Restrictions for APIs
Authentication
  • Setting Up Two-Factor Authentication
  • Mandatory Two-Factor Authentication
  • Single Sign-On
Specifications & Limitations
  • Recommended Environment
  • Limitations / Precautions
Content API
  • What is the Content API?
Basic Operations
  • GET /api/v1/{endpoint}
  • GET /api/v1/{endpoint}/{content_id}
  • POST /api/v1/{endpoint}
  • PUT /api/v1/{endpoint}/{content_id}
  • PATCH /api/v1/{endpoint}/{content_id}
  • DELETE /api/v1/{endpoint}/{content_id}
API Keys
  • API Key (API Authentication and Authorization Management)
  • Migration from the Old API (X-API-KEY, etc.)
Responses & Error Handling
  • Response format for each field in the GET API
  • Specifying Query Parameters in the GET API
  • Error Responses of the Content API
Management API (Beta)
  • POST /api/v1/roles
  • DELETE /api/v1/roles/{role_id}
  • GET /api/v1/roles/{role_id}
  • PATCH /api/v1/roles/{role_id}
  • GET /api/v1/roles
  • What is the Management API?
Content Operations
  • PUT /api/v1/contents/{endpoint}/{content_id}/reservation
  • GET /api/v1/contents/{endpoint}
  • GET /api/v1/contents/{endpoint}/{content_id}
  • PATCH /api/v1/contents/{endpoint}/{content_id}/status
  • PATCH /api/v1/contents/{endpoint}/{content_id}/createdBy
Media Operations
  • GET /api/v2/media
  • GET /api/v1/media
  • POST /api/v1/media
  • DELETE /api/v2/media
Member Operations
  • PATCH /api/v1/members/{member_id}
  • GET /api/v1/members
  • GET /api/v1/members/{member_id}
API Operations
  • POST /api/v1/apis
  • GET /api/v1/apis
  • GET /api/v1/apis/{endpoint}
Service Operations
  • GET /api/v1/service
Responses & Error Handling
  • Management API Error Responses
Image API
  • What is the Image API?
Parameters
  • Image Size
  • Quality
  • Resolution and Screen Size
  • Format
  • Text
  • Border
  • Padding
  • Shape
  • Download
  • Watermark
MCP Server
  • microCMS MCP
  • microCMS Document MCP Server
  • Top
  • Tutorial
  • Astro

Getting Started

Last updated: 2026/08/10

This document outlines the steps to use microCMS with Astro.
If you are using microCMS from Astro for the first time, please try the following steps first.

Prerequisites

  • You need Node.js and npm to develop with Astro. Please install them in your development environment.
  • You must have completed the creation of a microCMS service. If you haven't done this, please refer to the "Getting Started" document.

Setting Up microCMS

First, let's create a simple API in microCMS. This time, we will create it with the following configuration.

  • API Type: Object format
  • Endpoint: hello
  • Field Type: Text Field
  • Field ID: text


In the API creation screen, enter any API name and endpoint.
Please enter hello for the endpoint.



Next, select "Object format" as the API type.



Finally, set up the field. This time, we will create one text field as follows.

  • Field ID: text
  • Display Name: Text
  • Type: Text Field




Once you create the API with the above settings, you will be able to submit content.
Move to the editing screen, enter any value in the text field, and publish the content.
For example, enter the following content.

Hello, microCMS!!




Now, you are able to retrieve content from the API.
Click on "API Preview" in the upper right corner to access the created API and confirm that a JSON response is returned.

Preparing the Astro Project

Next, we will create an Astro project.
We will use the CLI officially provided by Astro. Please run the following command in your terminal.

npm create astro@latest


When you run npm create astro@latest, the necessary items for creating the Astro project will be displayed interactively.

This time, we will proceed with the following settings.

  • Project creation destination: astro-microcms
  • Template: Minimal template
  • Install dependencies: Yes
  • Initialize Git repository: Optional

The prompts may vary depending on the version of the CLI. Please choose according to your environment.



Once the project creation is complete, navigate to the project directory.

cd astro-microcms

Next, start the development server.

npm run dev

Access http://localhost:4321 from your browser.
If the project creation is successful, the initial screen of Astro will be displayed in the browser.


Initializing microcms-js-sdk

Install the JavaScript SDK microcms-js-sdk officially provided by microCMS.

npm install microcms-js-sdk

By using microcms-js-sdk, you can create a client with the createClient function and retrieve content from the microCMS API.

Setting Environment Variables

Create a .env file in the root directory of the project.

astro-microcms/
├── src/
├── .env
├── astro.config.mjs
└── package.json

Set the service domain and API key in the .env file.

MICROCMS_SERVICE_DOMAIN=service-domain
MICROCMS_API_KEY=api-key


For MICROCMS_SERVICE_DOMAIN, set the service domain of microCMS.
For example, if the URL of the Administration console is "https://example.microcms.io", specify example as the service domain.

For MICROCMS_API_KEY, set the API key that can be confirmed in the microCMS Administration console.

Creating the API Client

Create a lib directory inside the src directory and create a file named microcms.ts.

src/
├── lib/
│   └── microcms.ts
└── pages/
    └── index.astro

Write the following code in src/lib/microcms.ts.

import { createClient } from 'microcms-js-sdk';

const serviceDomain = import.meta.env.MICROCMS_SERVICE_DOMAIN;
const apiKey = import.meta.env.MICROCMS_API_KEY;

if (!serviceDomain || !apiKey) {
  throw new Error(
    'MICROCMS_SERVICE_DOMAIN or MICROCMS_API_KEY is not set.',
  );
}

export const client = createClient({
  serviceDomain,
  apiKey,
});

We are creating the microCMS API client by passing the service domain and API key to the createClient function.

Fetching and Displaying Information from microCMS

We will fetch information from the microCMS API we just created and display it on the screen.
Change src/pages/index.astro to the following content.

---
import { client } from '../lib/microcms';

type Hello = {
  text: string;
};

const data = await client.getObject<Hello>({
  endpoint: 'hello',
});
---

<html lang="ja">
  <head>
    <meta charset="UTF-8" />
    <meta
      name="viewport"
      content="width=device-width"
    />
    <title>Astro × microCMS</title>
  </head>
  <body>
    <main>
      <h1>{data.text}</h1>
    </main>
  </body>
</html>


The section at the top of the Astro file, surrounded by ---, is called the component script.
In the component script, we are fetching content from the microCMS API.

Since the API we created this time is in object format, we will use the getObject method of microcms-js-sdk. By specifying the Hello type, we also define the type of the text field to be retrieved.

const data = await client.getObject<Hello>({
  endpoint: 'hello',
});

The text of the retrieved data will be displayed in the HTML h1 element.

<h1>{data.text}</h1>

If the development server is not running, execute the following command.

npm run dev


Please access http://localhost:4321 from your browser.
The content submitted to microCMS will be displayed on the screen.



Change the text in the microCMS Administration console and publish it, then refresh the browser to confirm that the displayed content changes.

Finally, if the production build completes successfully, the initial setup is complete.

npm run build
  • Integration with Astro
  • Tutorial

Contents

  • Prerequisites
  • Setting Up microCMS
  • Preparing the Astro Project
  • Initializing microcms-js-sdk
  • Setting Environment Variables
  • Creating the API Client
  • Fetching and Displaying Information from microCMS
  • Company
  • Legal Notice
  • Terms of Service
  • Privacy Policy

Copyright © microcms.io All rights reserved.