Skip to main content

API v3 Basics

This document provides an overview of the basic concepts and features of the API v3.

In This Document

Getting Started

info

API version V3 is based on a different approach compared to V1, with new concepts designed for better management of web services. This document introduces these concepts and provides guidance on how to develop with them.

In this version, two key concepts are used: Transformers and Repositories. These components form the foundation of the API v3 architecture.

URL Structure

The API v3 follows a consistent URL structure pattern:

https://api.example.com/api/{audience}/{version}/{module}/{resource}/{id?}

Where:

  • audience: Indicates the target audience (client or admin)
  • version: API version (v3)
  • module: The module name (e.g., cms, user, system)
  • resource: The specific resource (e.g., post, category, user)
  • id: Optional resource identifier for specific resource operations

Examples

Client Endpoints
# List all posts
GET https://api.example.com/api/client/v3/cms/post

# Get a specific post
GET https://api.example.com/api/client/v3/cms/post/123

Response Format

All API responses follow a consistent JSON format:

Success Response
{
"status": true,
"result": {
// Response data
},
"meta": {
// Metadata, pagination, etc.
}
}

Core Concepts

Transformers

Transformers are an alternative to Laravel's Resources but with enhanced features. They handle the presentation layer of your API, determining how data is formatted before being sent as a response.

The API v3 uses the thephpleague/fractal package to implement transformers, providing a powerful way to format and structure API responses.

Key Features:

  • Consistent response formatting
  • Inclusion of related resources
  • Data serialization
  • Custom data presentation

Resources:

tip

For detailed information on implementing transformers, see the Transformers documentation.

Repositories

Repositories encapsulate database operations, providing a clean API for data access. They abstract the underlying Eloquent ORM, promoting code organization and maintenance.

The API v3 uses the andersao/l5-repository package to implement the repository pattern, offering convenient tools for CRUD operations and query customization.

/app/Repositories/PostRepository.php
class PostRepository extends BaseRepository
{
public function model()
{
return Post::class;
}

public function getPublished()
{
return $this->findWhere(['status' => 'published']);
}
}

Key Benefits:

  • Separation of concerns
  • Code reusability
  • Standardized data access
  • Enhanced testability
  • Simplified query logic

Resources:

tip

For detailed information on implementing repositories, see the Repositories documentation.

Filtering and Searching

The API v3 leverages the repository pattern to implement powerful filtering, searching, and pagination features. This provides a consistent way to query data across all endpoints.

Common Query Parameters

ParameterTypeDescriptionExample
searchstringSearch terms with format field:valuesearch=title:welcome
searchFieldsstringFields to search insearchFields=title;content
searchJoinstringHow to join multiple search terms (and or or)searchJoin=and
filterarrayFilter by specific field valuesfilter[]=status:published
orderBystringField to order results byorderBy=created_at
sortedBystringDirection to order results (asc or desc)sortedBy=desc
includesstringRelated entities to include in the responseincludes=author,category,tags
excludesstringFields to exclude from the responseexcludes=content
witharrayRelationships to includewith[]=author
withCountarrayCount of related entitieswithCount[]=comments
skipintegerNumber of records to skipskip=10
limitintegerMaximum number of records to returnlimit=25
pageintegerPage number for paginationpage=2
per_pageintegerNumber of items per pageper_page=20

Example Queries

Search Query
# Search for posts with "API" in the title
GET /api/client/v3/cms/post?search=API&searchFields=title

Rate Limiting

warning

API requests are subject to rate limiting to ensure fair usage and system stability.

Rate limit information is included in the response headers:

X-RateLimit-Limit: 60
X-RateLimit-Remaining: 59
X-RateLimit-Reset: 1625097600

Error Handling

The API uses standard HTTP status codes to indicate the success or failure of a request:

Status CodeDescription
200OK - The request was successful
400Bad Request - The request was invalid
401Unauthorized - Authentication failed
403Forbidden - The user does not have permission
404Not Found - The resource was not found
422Unprocessable Entity - Validation errors
429Too Many Requests - Rate limit exceeded
500Internal Server Error - Server error

Error responses include detailed information about the error:

Validation Error Example
{
"status": false,
"error": {
"code": "validation_error",
"message": "The given data was invalid.",
"details": {
"title": ["The title field is required."]
}
}
}