Flotiq & Astro
Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.
Flotiq is a headless CMS designed for various frontends, such as static sites, mobile, and web applications. Developers and content creators manage and deliver content through REST and GraphQL-based APIs.
Integrating with Astro
Section titled Integrating with AstroThis guide will use the Flotiq headless CMS API with an Astro project to display content on your website.
Prerequisites
Section titled PrerequisitesTo get started, you will need:
- An Astro project - You can create a new project using the
npm create astro@latest
command. - A Flotiq account - If you don’t have an account, sign up for free.
- Flotiq read-only API key - Find out how to obtain your key.
Setting up the Environment variables
Section titled Setting up the Environment variablesAdd the read-only API key from your Flotiq account to the .env
file in the root of your Astro project:
FLOTIQ_API_KEY=__YOUR_FLOTIQ_API_KEY__
Defining a Content Type in Flotiq
Section titled Defining a Content Type in FlotiqFirst, you need to define an example Content Type Definition in Flotiq to store data.
Log in to your Flotiq account and create a custom Content Type Definition with the following example Blog Post
configuration:
- Label: Blog Post
- API Name: blogpost
- Fields:
- Title: text, required
- Slug: text, required
- Content: rich text, required
Then, create one or more example Content Objects using this Blog Post
type.
Installing the Flotiq TypeScript SDK
Section titled Installing the Flotiq TypeScript SDKTo connect your project with Flotiq, install the Flotiq SDK using the package manager of your choice:
npm install flotiq-api-ts
pnpm add flotiq-api-ts
yarn add flotiq-api-ts
Next, configure the SDK using your credentials. Create a new file named flotiq.ts
inside the src/lib
directory of your project:
import { FlotiqApi } from "flotiq-api-ts";
export const flotiq = new FlotiqApi(import.meta.env.FLOTIQ_API_KEY);
This configuration can now be used throughout your project.
Fetching and Displaying Data from Flotiq
Section titled Fetching and Displaying Data from Flotiq-
Fetch the
Blog Post
data on an Astro page using your content’s custom APIBlogpostAPI
:src/pages/index.astro ---import { flotiq } from "../lib/flotiq";const posts = await flotiq.BlogpostAPI.list();--- -
Display the content in your Astro template. You will have access to the
title
,slug
, andcontent
of your posts as well as otherinternal
post data:src/pages/index.astro ---import { flotiq } from "../lib/flotiq";const posts = await flotiq.BlogpostAPI.list();---<html lang="en"><head><title>Astro</title></head><body>{posts.data?.map((post) => (<section><a href={`/posts/${post.slug}`}><h2>{post.title}</h2></a><div>{post.internal?.createdAt}</div><div set:html={post.content}/></section>))}</body></html>