# Skill: API Endpoint Creation (Express + Zod)

## Identity
- **Skill ID**: `api-endpoint-creation`
- **Domain**: RESTful API Endpoints
- **Technologies**: Express.js, Zod Validation, TypeScript
- **Source Agent**: `backend-app-expert.md`

## When to Load This Skill
- Creating REST API endpoints
- Request validation with Zod
- Error handling
- Files matching: `press-zone-backend/api/src/routes/**/*.ts`, `press-zone-backend/api/src/controllers/**/*.ts`

## Core Patterns

### Express Router Setup
```typescript
import express from 'express';
import { authenticateApiKey } from '../middleware/auth';
import { validateRequest } from '../middleware/validation';
import { translationSchema } from '../schemas/translation';

const router = express.Router();

router.post('/translate', 
  authenticateApiKey,
  validateRequest(translationSchema),
  async (req, res) => {
    try {
      const { source_lang, target_lang, content, model } = req.body;
      
      // Business logic here
      const result = await translateService.process({
        userId: req.user.id,
        sourceLang: source_lang,
        targetLang: target_lang,
        content,
        model
      });
      
      res.json({ success: true, data: result });
    } catch (error) {
      res.status(500).json({ success: false, error: error.message });
    }
  }
);

export default router;
```

### Zod Schema Validation
```typescript
import { z } from 'zod';

export const translationSchema = z.object({
  source_lang: z.string().length(2),
  target_lang: z.string().length(2),
  content: z.string().min(1).max(50000),
  model: z.enum(['4b', '27b']).default('4b'),
  tone: z.enum(['neutral', 'formal', 'casual']).optional(),
  callback_url: z.string().url().optional()
});

export type TranslationRequest = z.infer<typeof translationSchema>;
```

### Validation Middleware
```typescript
import { Request, Response, NextFunction } from 'express';
import { ZodSchema } from 'zod';

export const validateRequest = (schema: ZodSchema) => {
  return (req: Request, res: Response, next: NextFunction) => {
    try {
      schema.parse(req.body);
      next();
    } catch (error) {
      res.status(400).json({
        success: false,
        error: 'Validation failed',
        details: error.errors
      });
    }
  };
};
```

## Validation Checklist
- [ ] All endpoints have authentication middleware
- [ ] Request bodies validated with Zod schemas
- [ ] Error responses use consistent format
- [ ] Rate limiting applied
- [ ] CORS configured properly
