Docs
Environment Config
Documentation

Environment Config

Auto-configure the SDK from environment variables with createAutoBlogWriterFromEnv.

Environment Config

The createAutoBlogWriterFromEnv() function creates a fully configured SDK instance by reading environment variables. This is the foundation of the @autoblogwriter/sdk/next helpers.

Usage

import { createAutoBlogWriterFromEnv } from "@autoblogwriter/sdk";
 
const env = createAutoBlogWriterFromEnv();
 
// Access the client
const { posts } = await env.client.getPosts();
 
// Access configuration
console.log(env.workspaceSlug); // "my-blog"
console.log(env.siteUrl);      // "https://mysite.com"

Environment Variables

VariableRequiredDefaultDescription
AUTOBLOGWRITER_API_KEYYesWorkspace API key
AUTOBLOGWRITER_WORKSPACE_SLUGYesWorkspace slug
AUTOBLOGWRITER_API_URLNohttps://api.autoblogwriter.appAPI base URL
AUTOBLOGWRITER_WORKSPACE_IDNoWorkspace ID (optional)
SITE_URLNohttp://localhost:3000Your site URL (for sitemap/robots)
NEXT_PUBLIC_SITE_URLNoFalls back to SITE_URL
AUTOBLOGWRITER_REVALIDATE_SECRETNoWebhook HMAC secret

Return Value

createAutoBlogWriterFromEnv() returns a AutoBlogWriterEnvConfig object:

interface AutoBlogWriterEnvConfig {
  client: AutoBlogWriterClient;           // Pre-configured client instance
  workspaceSlug: string;
  workspaceId?: string;
  siteUrl: string;
  revalidateSecret?: string;
  apiUrl: string;
  apiKey: string;
  tags: {
    posts: string;                  // e.g. "autoblogwriter:my-blog:posts"
    post: (slug: string) => string; // e.g. "autoblogwriter:my-blog:post:hello-world"
    sitemap: string;                // e.g. "autoblogwriter:my-blog:sitemap"
  };
}

Cache Tags

The tags object provides pre-formatted Next.js cache tags:

const env = createAutoBlogWriterFromEnv();
 
// Use tags for granular cache invalidation
const { posts } = await env.client.getPosts({
  next: { tags: [env.tags.posts] },
});
 
const post = await env.client.getPostBySlug("hello-world", {
  next: { tags: [env.tags.post("hello-world")] },
});

Tag format:

  • All posts: autoblogwriter:{workspaceSlug}:posts
  • Single post: autoblogwriter:{workspaceSlug}:post:{postSlug}
  • Sitemap: autoblogwriter:{workspaceSlug}:sitemap

These tags are automatically used by the Next.js helpers and invalidated by the revalidation handler.

Singleton Pattern

createAutoBlogWriterFromEnv() uses a singleton — the first call creates the instance, subsequent calls return the same one. This avoids re-reading env vars and re-creating clients on every request.

// These return the same instance:
const a = createAutoBlogWriterFromEnv();
const b = createAutoBlogWriterFromEnv();
console.log(a === b); // true

When to Use

ScenarioUse
Next.js App Router with standard env varscreateAutoBlogWriterFromEnv() (or the @autoblogwriter/sdk/next helpers that call it for you)
Custom configuration or non-Next.jscreateAutoBlogWriterClient() directly
TestingcreateAutoBlogWriterClient() with a mock fetch

Next Steps