Official TypeScript SDK for interacting with the ContentaGen API.
- Lightweight client for ContentaGen API
- Input validation with Zod schemas and shared schema exports
- Automatic date parsing for
createdAt/updatedAt - Consistent error codes and robust error handling
- Locale support via
x-localeheader for internationalization - Agent-aware author, content, and related slug helpers
- Streaming AI assistant responses with runtime context awareness
- Image fetch helpers that proxy assets as base64 payloads
npm:
npm install @contentagen/sdkyarn:
yarn add @contentagen/sdkimport { createSdk } from "@contentagen/sdk";
const sdk = createSdk({
apiKey: "YOUR_API_KEY",
locale: "en-US", // Optional: sets the x-locale header for all requests
host: "https://custom.api.example.com", // Optional: custom API host
});
async function example() {
const agentId = "00000000-0000-0000-0000-000000000000";
// List content by agent(s)
const listParams = {
agentId, // required agent ID
status: ["approved", "draft"], // optional: string or array, defaults to approved
limit: 10, // optional, default 10
page: 1, // optional, default 1
};
const list = await sdk.listContentByAgent(listParams);
// Use list.total and list.posts[0] as needed
// Get content by slug (use the same agentId you queried with)
const selectParams = { slug: "my-post-slug", agentId };
const post = await sdk.getContentBySlug(selectParams);
// Use post.id, post.meta.title, post.createdAt, post.shareStatus as needed
// Get related slugs for a post
const relatedSlugs = await sdk.getRelatedSlugs({ slug: "my-post-slug", agentId });
// Use relatedSlugs array as needed
// Get author info by agent ID
const author = await sdk.getAuthorByAgentId({ agentId });
// Use author.name and author.profilePhoto as needed
// Get the image data for a specific content ID
const image = await sdk.getContentImage({ contentId: post.id });
// Use image?.contentType and image?.data as needed
// Get assistant response
const stream = sdk.streamAssistantResponse({
message: "Hello, assistant!",
agentId,
language: "en", // Optional, defaults to "en"
});
for await (const chunk of stream) {
// Consume streaming chunks as they arrive
}
}createSdk(config: { apiKey: string; locale?: string; host?: string }): ContentaGenSDK— factory for SDK instanceContentaGenSDKclass — all methods available on instancestype ShareStatus— union type:"private" | "shared"- Zod schemas for advanced validation:
ContentListResponseSchemaContentSelectSchemaGetContentBySlugInputSchemaListContentByAgentInputSchemaAuthorByAgentIdSchemaImageSchemaShareStatusValuesStreamAssistantResponseInputSchema
Note: The PostHog helper is currently internal and not exported from the package entry. See "PostHog Analytics Helper" below for usage details when working inside this repository.
-
sdk.listContentByAgent(params)- params (validated by
ListContentByAgentInputSchema): -agentId: string (UUID) — required -status: "draft" | "approved" | array of the same — optional, defaults to approved on the APIlimit?: number— optional, default 10, between 1 and 100page?: number— optional, default 1, min 1
- Returns:
Promise<{ posts: Array<{ id, meta, imageUrl, status, shareStatus, createdAt, stats, image }>; total: number }>
- params (validated by
-
sdk.getContentBySlug(params)- params (validated by
GetContentBySlugInputSchema):slug: string — requiredagentId: string (UUID) — required
- Returns:
Promise<ContentSelect-like object>(see Types below)
- params (validated by
-
sdk.getRelatedSlugs(params)- params:
{ slug: string; agentId: string } - Returns:
Promise<string[]>(array of related slugs)
- params:
-
sdk.getAuthorByAgentId(params)- params:
{ agentId: string } - Returns:
{ name: string; profilePhoto: { data: string; contentType: string } | null } - Note: The agent serves as the author. The returned name and profile photo are derived directly from the agent config.
- params:
-
sdk.getContentImage(params)- params:
{ contentId: string } - Returns:
{ data: string; contentType: string } | null
- params:
-
sdk.streamAssistantResponse(params)- params (validated by
StreamAssistantResponseInputSchema):message: string — requiredagentId: string — requiredlanguage: string — optional, defaults to"en"
- Returns:
AsyncGenerator<string>— yields streaming chunks from the assistant - Note: Iterate with
for await ... ofto consume the response stream
- params (validated by
The codebase includes a PostHog helper for tracking blog post views and custom events, designed for build-time frameworks like AstroJS.
Note: In v0.11.0 the helper is not exported through the package entry. If you are working inside this repo (or until a public export is added), import it directly from the source file.
import { createPostHogHelper } from "./src/posthog"; // not exported from the package entry yetposthogHelper.trackBlogPostView(postData)- params:
{ id: string; slug: string; title?: string; agentId: string } - Returns:
string— HTML script tag to track a blog post view event
- params:
- Includes stable identifiers for the post (
post_id,post_slug) and agent (agent_id). - May include optional metadata such as
post_title. - Includes event classification fields and a timestamp.
- Security: JSON payload is escaped for safe HTML injection.
Shapes shown here reflect the runtime Zod schemas returned by the SDK. Only ShareStatus is exported as a type; use the schemas above for runtime validation if needed.
-
ContentList
- posts: Array of summary objects:
id: stringmeta: { title?: string; description?: string; keywords?: string[]; slug?: string; sources?: string[] }imageUrl: string | nullstatus: "draft" | "approved"shareStatus: "private" | "shared"
stats: { wordsCount?: string; readTimeMinutes?: string; qualityScore?: string }createdAt: Dateimage: { data: string; contentType: string } | null
- total: number
- posts: Array of summary objects:
-
ContentSelect
id: stringagentId: stringimageUrl: string | nullbody: stringstatus: "draft" | "approved"shareStatus: "private" | "shared"
meta: { title?: string; description?: string; keywords?: string[]; slug?: string; sources?: string[] }request: { description: string; layout: "tutorial" | "interview" | "article" | "changelog" }stats: { wordsCount?: string; readTimeMinutes?: string; qualityScore?: string }createdAt: DateupdatedAt: Dateimage: { data: string; contentType: string } | null
-
AuthorByAgentId
name: stringprofilePhoto: { data: string; contentType: string } | null
-
RelatedSlugsResponse
- Array of strings (slugs)
-
ShareStatus
"private" | "shared"
SDK_E001: apiKey is required to initialize the ContentaGenSDKSDK_E002: API request failedSDK_E003: Invalid API response formatSDK_E004: Invalid input
async function run() {
try {
const sdk = createSdk({ apiKey: process.env.CONTENTAGEN_API_KEY! });
const agentId = "00000000-0000-0000-0000-000000000000";
const list = await sdk.listContentByAgent({
agentId,
status: "approved",
limit: 5,
page: 1,
});
if (list.total === 0) {
// Handle no posts found
return;
}
const first = list.posts[0];
// Use first.id as needed
const post = await sdk.getContentBySlug({
slug: first.meta.slug ?? "unknown-slug",
agentId,
});
// Use post.body as needed
} catch (err) {
console.error("SDK error:", err);
}
}import { createSdk } from "@contentagen/sdk";
import { createPostHogHelper } from "./src/posthog"; // not exported from the package entry yet
const sdk = createSdk({ apiKey: "YOUR_API_KEY" });
const posthogHelper = createPostHogHelper();
// Example usage in AstroJS or other build-time frameworks
async function renderBlogPost(slug: string, agentId: string) {
// Get post data
const post = await sdk.getContentBySlug({ slug, agentId });
// Generate tracking script for blog post view
const trackViewScript = posthogHelper.trackBlogPostView({
id: post.id,
slug: post.meta.slug || slug,
title: post.meta.title,
agentId: post.agentId,
});
// In AstroJS, inject the tracking script into the page
return {
trackingScript: trackViewScript,
post,
};
}See CHANGELOG.md for version history and updates.
Apache License 2.0