Take-home assignment

Take-Home Assignment: REST API CRUD with Authentication

A backend take-home guide: API design, validation, token authentication, tests, README template and deploying to Render, with Laravel examples that apply to any stack.

5 min read

Backend take-home tasks usually ask for a small REST API with CRUD and authentication, for example "a notes or products API where users can only manage their own records". Examples here use Laravel, but the checklist applies to Node.js/Express, Django or any other stack.

What reviewers actually check#

  • Correct HTTP methods and status codes.
  • Validation with helpful error messages.
  • Authentication and authorisation (users cannot touch other users' data).
  • Clean structure: thin controllers, no business logic in routes.
  • Tests for the main flows.
  • A README that lets them run everything quickly, plus a live URL if possible.

Step 1: Design the endpoints first#

Write the API contract before coding and put it in the README:

MethodEndpointAuthDescriptionSuccess
POST/api/registernocreate an account, return a token201
POST/api/loginnoreturn a token200
POST/api/logoutyesrevoke the current token204
GET/api/notesyeslist my notes (paginated)200
POST/api/notesyescreate a note201
GET/api/notes/{id}yesshow one of my notes200
PATCH/api/notes/{id}yesupdate one of my notes200
DELETE/api/notes/{id}yesdelete one of my notes204

Use a consistent JSON shape, and return 401 (not logged in), 403 (not your note), 404 (not found) and 422 (validation) correctly.

Step 2: Folder structure#

text
app/
├── Http/
│   ├── Controllers/Api/
│   │   ├── AuthController.php
│   │   └── NoteController.php
│   ├── Requests/
│   │   ├── StoreNoteRequest.php
│   │   └── UpdateNoteRequest.php
│   └── Resources/
│       └── NoteResource.php
├── Models/
│   ├── Note.php
│   └── User.php
└── Policies/
    └── NotePolicy.php
database/
├── factories/NoteFactory.php
└── migrations/xxxx_create_notes_table.php
routes/api.php
tests/Feature/
├── AuthTest.php
└── NoteTest.php

Step 3: Validation, authorisation and resources#

php
class StoreNoteRequest extends FormRequest
{
    public function rules(): array
    {
        return [
            'title' => ['required', 'string', 'max:150'],
            'body' => ['nullable', 'string', 'max:5000'],
        ];
    }
}

class NoteController extends Controller
{
    public function store(StoreNoteRequest $request): NoteResource
    {
        $note = $request->user()->notes()->create($request->validated());

        return new NoteResource($note); // 201 Created for a new model
    }

    public function update(UpdateNoteRequest $request, Note $note): NoteResource
    {
        $this->authorize('update', $note); // NotePolicy: $user->id === $note->user_id

        $note->update($request->validated());

        return new NoteResource($note);
    }
}
  • Use API Resources so you never leak fields such as passwords or internal flags.
  • Use a Policy for ownership checks instead of if statements in every method.
  • Paginate lists: NoteResource::collection($request->user()->notes()->latest()->paginate(15)).

Step 4: Authentication#

Use the framework's standard tool: Laravel Sanctum tokens ($user->createToken('api')->plainTextToken), or JWT/sessions in other stacks. Hash passwords (Hash::make / bcrypt), never return them, and rate-limit the login route (throttle:5,1).

Step 5: Tests for the main flows#

php
it('does not let a user update someone else\'s note', function () {
    $owner = User::factory()->create();
    $note = Note::factory()->for($owner)->create();

    Sanctum::actingAs(User::factory()->create());

    $this->patchJson("/api/notes/{$note->id}", ['title' => 'Hacked'])
        ->assertForbidden();
});

it('validates the title', function () {
    Sanctum::actingAs(User::factory()->create());

    $this->postJson('/api/notes', ['title' => ''])
        ->assertUnprocessable()
        ->assertJsonValidationErrors('title');
});

Cover at least: register/login, guests get 401, create/list/update/delete, validation errors and the ownership rule.

Step 6: Commits#

text
chore: install Laravel and Sanctum
feat: register, login and logout endpoints
feat: notes CRUD with validation and resources
feat: restrict notes to their owner with a policy
test: auth and notes feature tests
docs: README with endpoints and setup

Step 7: README template#

markdown
# Notes API

Live API: https://notes-api.onrender.com/api
Postman collection: docs/notes.postman_collection.json

## Run locally
```bash
cp .env.example .env
composer install
php artisan key:generate
php artisan migrate --seed
php artisan serve
php artisan test
```

Demo user: demo@example.com / password

## Endpoints
(table from Step 1)

## Decisions and trade-offs
- Sanctum tokens because the brief mentions a mobile client.
- Soft deletes not added: not required by the brief.

## What I would add with more time
- Search and tags
- API documentation with OpenAPI

Step 8: Deploy checklist (Render)#

  1. Push to GitHub and create a Web Service on Render from the repository (Docker or a PHP build), plus a managed database (or any free MySQL/PostgreSQL host).
  2. Set the environment variables: APP_KEY, APP_ENV=production, APP_DEBUG=false, DB_*.
  3. Run php artisan migrate --force as part of the deploy (a release or start command).
  4. Test every endpoint against the live URL with Postman, including a 401 and a 422 case.
  5. Free instances sleep when idle, so mention in your email that the first request can take up to a minute.

Never commit .env or real credentials, and make sure APP_DEBUG is false in production so stack traces are not exposed.

Step 9: Submit#

Send the repository link, live URL, Postman collection and demo credentials in one short email, before the deadline. List any assumptions and known limitations in the README.

All guides