Skip to main content

Get Up and Running

This guide will have you uploading files through API Master in less than 5 minutes.
1

Clone or Download the Project

Get the source code on your local machine:
# If using git
git clone <your-repository-url>
cd api-master

# Or download and extract the ZIP file
The project contains all necessary TypeScript configuration and dependencies defined in package.json.
2

Install Dependencies

Install all required packages using npm:
npm install
This will install:
  • express - Web framework
  • cors - CORS middleware
  • multer - File upload handling
  • TypeScript and all type definitions
Using Node.js v16 or higher is recommended for best compatibility.
3

Build the Project

Compile TypeScript to JavaScript:
npm run build
This creates the dist/ directory with compiled JavaScript files.
4

Start the Server

Launch the API server:
npm start
You should see:
Server running on port 3000
By default, the server runs on port 3000. You can change this by setting the PORT environment variable.
5

Test Your First Upload

Upload a file using curl or any HTTP client:
curl -X POST http://localhost:3000/users/upload \
  -F "file=@/path/to/your/file.jpg"
Expected Response:
{
  "message": "File uploaded successfully",
  "url": "http://localhost:3000/uploads/file-1234567890-abc123xyz.jpg"
}
The returned URL is publicly accessible - try opening it in your browser to view the uploaded file!

Verify Everything Works

Test the welcome endpoint to confirm the API is running:
curl http://localhost:3000/
Response:
{
  "message": "Welcome to API Master"
}

Understanding the Upload Flow

Here’s how API Master handles file uploads under the hood:
import express from 'express';
import userController from '../controllers/userController';
import multer from 'multer';
import path from 'path';

const router = express.Router();

// Multer configuration
const storage = multer.diskStorage({
  destination: (req, file, cb) => {
    cb(null, path.join(__dirname, '../../uploads'));
  },
  filename: (req, file, cb) => {
    const uniqueName = 'file-' + Date.now() + '-' + Math.random().toString(36).substr(2, 9) + path.extname(file.originalname);
    cb(null, uniqueName);
  }
});
const upload = multer({ storage: storage });

router.post('/upload', upload.single('file'), userController.uploadFile);

export default router;
How it works:
  1. Multer intercepts the multipart/form-data request
  2. Files are stored in the uploads/ directory with unique names
  3. The controller generates a public URL pointing to the uploaded file
  4. Express serves uploaded files via the /uploads static route

Test with Other Tools

curl -X POST http://localhost:3000/users/upload \
  -F "[email protected]"

CORS is Pre-Configured

API Master comes with CORS enabled for all origins in development:
app.ts
const corsOptions = {
  origin: '*', // Allow all origins (development mode)
  methods: ['GET', 'POST', 'PUT', 'DELETE'],
  allowedHeaders: ['Content-Type', 'Authorization']
};

app.use(cors(corsOptions));
In production, you should restrict origin to specific domains for security. See the Configuration Guide for details.

Common Issues

Set a different port:
PORT=4000 npm start
Create the uploads directory manually:
mkdir uploads
Ensure your form field name is file or the request includes -F "file=@..." in curl.

Next Steps

Now that you have API Master running:

API Reference

Explore all available endpoints and parameters

Configuration

Customize CORS, file storage, and server settings

Deployment

Deploy to production environments

Error Handling

Handle errors and edge cases effectively
For development with auto-reload on code changes, use npm run dev instead of npm start.

Build docs developers (and LLMs) love