Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
Spatie Content Api

Spatie Content Api Laravel Package

spatie/spatie-content-api

Laravel/PHP package powering Spatie’s promotional-site content API. Fetch posts for a product or project (e.g., mailcoach) via a simple ContentApi facade and expose consistent, reusable content to your frontend or other services.

View on GitHub
Deep Wiki
Context7

The content API used at promotional sites for our own products

Frequently asked questions about Spatie Content Api
How do I fetch posts for a specific product (e.g., 'mailcoach') in Laravel using this package?
Use the `ContentApi` facade with a single method call: `ContentApi::getPosts('mailcoach')`. This retrieves all posts associated with that product, structured for easy consumption in your frontend or API clients. The package abstracts the underlying storage, so you only need to define the product slug.
Does this package work with Laravel 10+? Are there breaking changes from older versions?
Yes, the package is fully compatible with Laravel 10+. Spatie maintains backward compatibility for minor versions, but always check the [CHANGELOG](https://github.com/spatie/spatie-content-api/blob/main/CHANGELOG.md) for version-specific notes. Major Laravel upgrades may require testing due to dependency updates.
Can I use this for a headless CMS or API-driven frontend (React/Vue)?
Absolutely. The package is designed for API-first content delivery, making it perfect for headless setups. You can expose endpoints like `/api/posts/{product}` and consume them directly in SPAs or mobile apps. Pair it with Laravel’s built-in API resources or custom JSON responses for flexibility.
How do I customize the content model (e.g., add fields like 'publish_date' or 'author')?
Extend the base `Post` model (or create your own) using Eloquent. The package leverages Laravel’s ORM, so you can add custom fields, relationships, or validation rules. For example, add a `published_at` column and use Laravel’s accessors/mutators to format dates in API responses.
Is there built-in caching for high-traffic APIs? How can I optimize performance?
The package doesn’t include caching by default, but you can integrate Laravel’s cache (e.g., `Cache::remember`) or external solutions like Redis/Varnish. For example, cache responses for 5 minutes: `Cache::remember('posts_mailcoach', 300, fn() => ContentApi::getPosts('mailcoach'))`. Index database columns (e.g., `product_slug`) for faster queries.
How do I secure the API endpoints? Can I restrict access to specific clients (e.g., SPAs)?
Use Laravel’s built-in middleware (e.g., `auth:sanctum` for SPAs) or Spatie’s `laravel-permission` for role-based access. Add middleware to your API routes: `Route::middleware(['auth:sanctum'])->get('/posts/{product}', ...)`. For API keys, use Laravel’s `api` auth guard or packages like `spatie/laravel-api-token`.
Can I store content in JSON or NoSQL instead of a traditional database?
The package is database-agnostic and works with MySQL, PostgreSQL, or SQLite out of the box. For JSON storage, use Laravel’s `json` column type or serialize data in a text column. For NoSQL (e.g., MongoDB), you’d need to replace Eloquent with a custom repository or use a package like `jenssegers/laravel-mongodb` and adapt the package’s logic.
Are there alternatives to this package for Laravel content APIs?
For lightweight APIs, consider `spatie/laravel-activitylog` (for audit trails) or `spatie/laravel-medialibrary` (for media). For full-fledged CMS features, evaluate `spatie/laravel-medialibrary` + custom API routes, `filamentphp/filament` (admin panel + API), or `orchid/software` (modular CMS). This package excels in simplicity for promotional content.
How do I test the API responses in my Laravel app?
Use Laravel’s HTTP tests with `Http::fake()` to mock API calls or test routes directly. For example: `public function test_posts_endpoint() { $response = $this->get('/api/posts/mailcoach'); $response->assertJsonStructure(['data' => [['title', 'content']]]); }`. The package includes CI tests, but you’ll need to write application-specific tests for your models and routes.
Can I deploy this as a standalone microservice (e.g., Docker) or does it require the full Laravel app?
The package is designed to work within a Laravel app, but you can deploy the API routes independently by extracting them into a separate service with shared database access. Use Laravel’s `queue:work` or `schedule:run` commands in Docker for background tasks. For true microservices, consider splitting the API into a separate repo with shared models via a package or event-driven architecture.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport