npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

miniapi-framework

v1.2.6

Published

The smallest API framework (1KB) with zero dependencies

Readme

MiniAPI - The Lightweight Node.js API Framework

MiniAPI is a blazing-fast, ultra-lightweight API framework for Node.js. At just 1KB, it provides essential routing, middleware, error handling, and more – all without dependencies.

🚀 Why MiniAPI?

  • Ultra-Lightweight – Only 1KB in size!
  • Zero Dependencies – Pure Node.js HTTP module.
  • Express-Like API – Simple and intuitive routing.
  • Middleware Support – Custom middleware for flexibility.
  • Error Handling – Graceful error handling built-in.
  • Grouped Routes – Organize APIs with prefixes.
  • CORS Support – Enable Cross-Origin Resource Sharing.
  • Rate Limiting & Compression – Optimize performance.
  • Super Fast – Outperforms Express with minimal overhead.

💊 Performance Benchmarks

MiniAPI is significantly faster than Express:

| Framework | Requests/sec | Avg Response Time | | ----------- | ------------ | ----------------- | | MiniAPI | 12,000 | 3.20ms | | Express | 7,000 | 6.21ms |

Benchmark Setup:

  • 10,000 requests with 50 concurrent connections.
  • Lower response time = faster performance.

📦 Installation

npm install miniapi-framework

🛠 Basic Usage

import { createAPI } from 'miniapi-framework';

// Create API instance
const api = createAPI({
  logging: true,
  cors: true
});

// Define routes
api.get('/hello', (ctx) => {
  ctx.send({ message: 'Hello, World!' });
});

api.post('/echo', (ctx) => {
  ctx.send(ctx.body);
});

// Start the server
api.listen(3000, () => {
  console.log('Server running on http://localhost:3000');
});

⚡ Advanced Usage

Middleware Support

api.use(async (ctx, next) => {
  console.log(`Request: ${ctx.req.method} ${ctx.req.url}`);
  return next();
});

Route Parameters

api.get('/users/:id', (ctx) => {
  const userId = ctx.params.id;
  ctx.send({ id: userId, name: `User ${userId}` });
});

Grouped Routes

api.group('/api', (apiRouter) => {
  apiRouter.get('/users', (ctx) => {
    ctx.send([{ id: 1, name: 'User 1' }]);
  });
  
  apiRouter.get('/users/:id', (ctx) => {
    ctx.send({ id: ctx.params.id, name: `User ${ctx.params.id}` });
  });
});

Authentication Middleware

const auth = async (ctx, next) => {
  const token = ctx.req.headers['authorization'];
  
  if (!token || !token.startsWith('Bearer ')) {
    return ctx.send({ error: 'Unauthorized' }, 401);
  }
  
  // Validate token
  const apiKey = token.split(' ')[1];
  if (apiKey !== 'your-api-key') {
    return ctx.send({ error: 'Invalid API key' }, 401);
  }
  
  ctx.user = { id: 1, role: 'admin' };
  return next();
};

// Apply to specific routes
api.get('/protected', auth, (ctx) => {
  ctx.send({ message: 'Protected data', user: ctx.user });
});

Error Handling

api.onError((error, ctx) => {
  console.error('Error:', error);
  ctx.send({ error: 'Internal Server Error' }, 500);
  return true; // Error was handled
});

Response Methods

// JSON response (default)
ctx.send({ data: 'example' }, 200);

// Text response
ctx.text('Plain text response', 200);

// HTML response
ctx.html('<h1>Hello World</h1>', 200);

// Redirect
ctx.redirect('/new-location', 302);

🔐 API Reference

Creating an API Instance

const api = createAPI(options);

Options:

| Option | Description | Default | | ------------- | --------------------------- | ------- | | cors | Enable CORS | false | | logging | Enable request logging | false | | compression | Enable response compression | false | | rateLimit | Enable rate limiting | false | | rateWindow | Rate limit window in ms | 60000 | | rateMax | Maximum requests per window | 100 |

Routing Methods

api.get(path, handler);
api.post(path, handler);
api.put(path, handler);
api.delete(path, handler);
api.patch(path, handler);

Context Object Properties

| Property | Description | | -------- | -------------------------------- | | req | Original Node.js request object | | res | Original Node.js response object | | params | URL parameters | | query | Query string parameters | | body | Parsed request body |


🌐 Complete Example

import { createAPI } from 'miniapi-framework';

// Create API instance with options
const api = createAPI({
  logging: true,
  cors: true,
  compression: true,
  rateLimit: true
});

// Global middleware
api.use(async (ctx, next) => {
  ctx.requestTime = new Date();
  ctx.res.setHeader('X-Powered-By', 'MiniAPI');
  return next();
});

// Basic routes
api.get('/', (ctx) => {
  ctx.send({ 
    message: 'Welcome to MiniAPI!',
    timestamp: ctx.requestTime.toISOString()
  });
});

// API routes group
api.group('/api', (apiRouter) => {
  apiRouter.get('/users', (ctx) => {
    const users = [
      { id: 1, name: 'Alice' },
      { id: 2, name: 'Bob' }
    ];
    ctx.send({ users });
  });
  
  apiRouter.get('/users/:id', (ctx) => {
    const userId = ctx.params.id;
    ctx.send({ id: userId, name: `User ${userId}` });
  });
  
  apiRouter.post('/users', (ctx) => {
    if (!ctx.body || !ctx.body.name) {
      return ctx.send({ error: 'Name is required' }, 400);
    }
    
    ctx.send({ 
      message: 'User created',
      user: {
        id: 3,
        name: ctx.body.name,
        createdAt: new Date().toISOString()
      }
    }, 201);
  });
});

// Error handling
api.onError((error, ctx) => {
  console.error('Error:', error);
  ctx.send({ error: 'Internal Server Error' }, 500);
  return true;
});

// Start the server
api.listen(3000, (server) => {
  console.log('🚀 MiniAPI server running on http://localhost:3000');
});

💡 Why Choose MiniAPI Over Alternatives?

| Feature | MiniAPI | Express | Fastify | | ------------- | -------------- | ------- | ------- | | Size | 1KB | 60KB+ | 30KB+ | | Dependencies | 0 | Many | Some | | Performance | 🔥 Fastest | Medium | High | | Middleware | ✅ Yes | ✅ Yes | ✅ Yes | | CORS Support | ✅ Yes | ✅ Yes | ✅ Yes | | Rate Limiting | ✅ Yes | ❌ No | ✅ Yes |


🐝 License & Contributions

MiniAPI is open-source and licensed under MIT. Contributions are welcome!

Feel free to submit issues, feature requests, or PRs. Let's make MiniAPI even better! 🚀

🔧 Development

To build from source:

# Clone the repository
git clone https://github.com/yourusername/miniapi-framework.git
cd miniapi-framework

# Install development dependencies
npm install

# Build the package
npm run build

The build process generates both CommonJS (.cjs) and ES Module (.mjs) versions for maximum compatibility.