Skip to main content
POST
/
api
/
post
Create Post
curl --request POST \
  --url https://api.example.com/api/post \
  --header 'Content-Type: application/json' \
  --data '
{
  "json": "<string>"
}
'
{
  "code": 123,
  "status": "<string>",
  "post": {
    "id": 123,
    "user_id": 123,
    "category_id": 123,
    "title": "<string>",
    "content": "<string>",
    "image": "<string>",
    "created_at": "<string>",
    "updated_at": "<string>"
  },
  "message": "<string>"
}

Overview

This endpoint creates a new blog post. The authenticated user is automatically set as the post author based on the JWT token.
This endpoint requires authentication. You must include a valid JWT token in the Authorization header.

Endpoint

POST /api/post

Authentication

Required. This endpoint uses the api.auth middleware to verify the JWT token. Header:
Authorization: Bearer YOUR_JWT_TOKEN

Request Parameters

All parameters must be sent as a JSON string in the json field.
json
string
required
JSON-encoded string containing the post data
title
string
required
Title of the post
content
string
required
Content/body of the post
category_id
number
required
ID of the category this post belongs to
image
string
required
Filename of the uploaded image (use the upload endpoint first)

Example Request Body

{
  "json": "{\"title\":\"My New Post\",\"content\":\"This is the content of my post...\",\"category_id\":2,\"image\":\"1678901234photo.jpg\"}"
}
The user_id is automatically extracted from the JWT token and does not need to be provided in the request.

Response

Success Response (200)

code
number
required
HTTP status code (200 for success)
status
string
required
Status message (“success”)
post
object
required
The newly created post object
id
number
Unique identifier for the post
user_id
number
ID of the user who created the post (from JWT token)
category_id
number
ID of the post’s category
title
string
Title of the post
content
string
Content/body of the post
image
string
Filename of the post’s image
created_at
string
Timestamp when the post was created
updated_at
string
Timestamp when the post was last updated
{
  "code": 200,
  "status": "success",
  "post": {
    "id": 15,
    "user_id": 5,
    "category_id": 2,
    "title": "My New Post",
    "content": "This is the content of my post...",
    "image": "1678901234photo.jpg",
    "created_at": "2026-03-04T14:25:30.000000Z",
    "updated_at": "2026-03-04T14:25:30.000000Z"
  }
}

Error Response (400) - Validation Failed

code
number
HTTP status code (400 for bad request)
status
string
Status message (“error”)
message
string
Error description
{
  "code": 400,
  "status": "error",
  "message": "Faltan datos"
}

Error Response (400) - Invalid JSON

{
  "code": 400,
  "status": "error",
  "message": "Envia los datos correctamente"
}

Validation Rules

The following validation rules are enforced:
  • title: Required field
  • content: Required field
  • category_id: Required field
  • image: Required field

Implementation Details

The endpoint extracts the user ID from the JWT token:
$jwtAuth = new JwtAuth();
$token = $request->header('Authorization', null);
$user = $jwtAuth->checkToken($token, true);
$post->user_id = $user->sub;
Before creating a post, you should first upload an image using the Upload Image endpoint to get the image filename.

Build docs developers (and LLMs) love