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
| Variable | Required | Default | Description |
|---|---|---|---|
AUTOBLOGWRITER_API_KEY | Yes | — | Workspace API key |
AUTOBLOGWRITER_WORKSPACE_SLUG | Yes | — | Workspace slug |
AUTOBLOGWRITER_API_URL | No | https://api.autoblogwriter.app | API base URL |
AUTOBLOGWRITER_WORKSPACE_ID | No | — | Workspace ID (optional) |
SITE_URL | No | http://localhost:3000 | Your site URL (for sitemap/robots) |
NEXT_PUBLIC_SITE_URL | No | — | Falls back to SITE_URL |
AUTOBLOGWRITER_REVALIDATE_SECRET | No | — | Webhook 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); // trueWhen to Use
| Scenario | Use |
|---|---|
| Next.js App Router with standard env vars | createAutoBlogWriterFromEnv() (or the @autoblogwriter/sdk/next helpers that call it for you) |
| Custom configuration or non-Next.js | createAutoBlogWriterClient() directly |
| Testing | createAutoBlogWriterClient() with a mock fetch |
Next Steps
- Next.js Helpers — Higher-level functions that use
createAutoBlogWriterFromEnv()internally. - Client Reference — Manual client configuration.