Skip to content

jonaspauleta/symfony-fruits-api

Repository files navigation

Fruits API Documentation

This is a REST API built with Symfony and API Platform for managing fruits.

Base URL

http://127.0.0.1:8000/api

Endpoints

Get All Fruits

GET /api/fruits

Returns a list of all fruits.

Response:

{
  "@context": "/api/contexts/Fruit",
  "@id": "/api/fruits",
  "@type": "Collection",
  "totalItems": 20,
  "member": [
    {
      "@id": "/api/fruits/1",
      "@type": "Fruit",
      "id": 1,
      "name": "Apple"
    }
  ]
}

Get Single Fruit

GET /api/fruits/{id}

Returns a specific fruit by ID.

Response:

{
  "@context": "/api/contexts/Fruit",
  "@id": "/api/fruits/1",
  "@type": "Fruit",
  "id": 1,
  "name": "Apple"
}

Create New Fruit

POST /api/fruits

Creates a new fruit.

Headers:

  • Content-Type: application/ld+json

Request Body:

{
  "name": "Dragon Fruit"
}

Response (201 Created):

{
  "@context": "/api/contexts/Fruit",
  "@id": "/api/fruits/21",
  "@type": "Fruit",
  "id": 21,
  "name": "Dragon Fruit"
}

Update Fruit (Full Replace)

PUT /api/fruits/{id}

Updates a fruit completely.

Headers:

  • Content-Type: application/ld+json

Request Body:

{
  "name": "Updated Dragon Fruit"
}

Response (200 OK):

{
  "@context": "/api/contexts/Fruit",
  "@id": "/api/fruits/21",
  "@type": "Fruit",
  "id": 21,
  "name": "Updated Dragon Fruit"
}

Update Fruit (Partial)

PATCH /api/fruits/{id}

Partially updates a fruit.

Headers:

  • Content-Type: application/merge-patch+json

Request Body:

{
  "name": "Partially Updated Dragon Fruit"
}

Delete Fruit

DELETE /api/fruits/{id}

Deletes a fruit.

Response (204 No Content)

Custom Endpoints (via Custom Controller)

Search Fruits

GET /api/fruits/search?q={query}

Search for fruits by name (case-insensitive, partial match).

Example:

curl -X GET "http://127.0.0.1:8000/api/fruits/search?q=apple"

Response:

[
  {
    "id": 1,
    "name": "Apple"
  },
  {
    "id": 6,
    "name": "Pineapple"
  }
]

Validation Rules

  • name: Required, minimum 2 characters, maximum 255 characters

Error Responses

400 Bad Request (Validation Error)

{
  "error": "Validation failed",
  "violations": {
    "name": "Fruit name cannot be blank"
  }
}

404 Not Found

{
  "error": "Fruit not found"
}

415 Unsupported Media Type

{
  "@context": "/api/contexts/Error",
  "@type": "Error",
  "title": "An error occurred",
  "detail": "The content-type \"application/json\" is not supported. Supported MIME types are \"application/ld+json\".",
  "status": 415
}

cURL Examples

Get all fruits

curl -X GET http://127.0.0.1:8000/api/fruits

Get single fruit

curl -X GET http://127.0.0.1:8000/api/fruits/1

Create fruit

curl -X POST http://127.0.0.1:8000/api/fruits \
  -H "Content-Type: application/ld+json" \
  -d '{"name":"Mango"}'

Update fruit

curl -X PUT http://127.0.0.1:8000/api/fruits/1 \
  -H "Content-Type: application/ld+json" \
  -d '{"name":"Green Apple"}'

Delete fruit

curl -X DELETE http://127.0.0.1:8000/api/fruits/1

Search fruits

curl -X GET "http://127.0.0.1:8000/api/fruits/search?q=berry"

Getting Started

  1. Install dependencies:
composer install
  1. Set up the database:
php bin/console doctrine:migrations:migrate
  1. Load sample data:
php bin/console doctrine:fixtures:load
  1. Start the server:
symfony serve
  1. Access the API documentation at: http://127.0.0.1:8000/api

Features

  • ✅ Full CRUD operations (Create, Read, Update, Delete)
  • ✅ Input validation with detailed error messages
  • ✅ JSON-LD format support (API Platform standard)
  • ✅ Search functionality
  • ✅ Proper HTTP status codes
  • ✅ Serialization groups for clean API responses
  • ✅ Sample data fixtures for testing
  • ✅ Both API Platform automatic endpoints and custom controller endpoints

API Platform Integration

This API uses API Platform, which provides:

  • Automatic API documentation at /api
  • JSON-LD format support
  • Hydra vocabulary for API discoverability
  • OpenAPI/Swagger documentation
  • Automatic content negotiation

You can access the interactive API documentation by visiting http://127.0.0.1:8000/api in your browser.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published