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

@build0.ai/credentials

v1.1.2

Published

A higher-order function for Next.js API routes that automatically handles encrypted credential retrieval and injection

Readme

@build0.ai/credentials

A higher-order function for Next.js API routes that automatically handles encrypted credential retrieval and injection.

Features

  • 🔐 Automatic credential decryption using AES-256-GCM
  • 🚀 Environment variable fallback for improved performance
  • 🛡️ Type-safe credential handling with Zod validation
  • 🔄 Seamless integration with Next.js API routes
  • 📦 Zero-config setup

Installation

npm install @build0.ai/credentials

Quick Start

import { withCredentials, RequestWithCredentials } from '@build0.ai/credentials';

export const GET = withCredentials(async (request: RequestWithCredentials) => {
  // Access decrypted credentials
  const credentials = request.appCredentials;
  
  // Use credentials to make authenticated API calls
  return NextResponse.json({ status: 'success' });
});

Configuration

Environment Variables

The package requires the following environment variables:

# Required: Encryption key for credential decryption
ENCRYPTION_KEY=your-32-byte-hex-encryption-key

# Option 1: Pre-encrypted credentials (recommended for performance)
ENCRYPTED_APP_CREDENTIALS=iv:authTag:encryptedData

# Option 2: Fallback credential endpoint
GET_CREDENTIALS_URL=https://your-api.com/credentials

Credential Sources

The package supports two credential sources with automatic fallback:

  1. Environment Variable (Primary): ENCRYPTED_APP_CREDENTIALS

    • Fastest option - no HTTP requests
    • Ideal for production deployments
  2. HTTP Endpoint (Fallback): GET_CREDENTIALS_URL

    • Fetches credentials from remote endpoint
    • Forwards cookies for authentication
    • Used when ENCRYPTED_APP_CREDENTIALS is not available

API Reference

withCredentials(handler)

Higher-order function that wraps your API route handler.

Parameters:

  • handler: Your API route handler function

Returns:

  • Enhanced handler that provides appCredentials on the request object

RequestWithCredentials

Extended Next.js request interface with credentials attached.

Properties:

  • appCredentials: Decrypted and validated credentials object
  • All standard NextRequest properties

Credential Structure

Credentials follow this TypeScript interface:

type Credentials = Record<string, {
  type: string;      // Credential type
  provider: string;  // Credential provider
  [key: string]: any; // Additional provider-specific fields
}>;

Error Handling

The package throws CredentialError when:

  • Credentials cannot be decrypted
  • Network requests fail
  • Credential validation fails

API routes return a 401 status with error details:

{
  "error": "Credential access failed",
  "message": "Detailed error message"
}

Security

  • Uses AES-256-GCM encryption with authentication
  • Supports Additional Authenticated Data (AAD)
  • Validates credential structure with Zod schemas
  • Secure key management via environment variables

Example Usage

Basic API Route

// app/api/example/route.ts
import { withCredentials, RequestWithCredentials } from '@build0.ai/credentials';
import { NextResponse } from 'next/server';

export const GET = withCredentials(async (request: RequestWithCredentials) => {
  const { appCredentials } = request;
  
  // Access specific integration credentials
  const githubCreds = appCredentials.github;
  const slackCreds = appCredentials.slack;
  
  // Make authenticated API calls
  // ...
  
  return NextResponse.json({ message: 'Success' });
});

With Error Handling

export const POST = withCredentials(async (request: RequestWithCredentials) => {
  try {
    const { appCredentials } = request;
    // Your logic here
    return NextResponse.json({ success: true });
  } catch (error) {
    return NextResponse.json(
      { error: 'Internal server error' },
      { status: 500 }
    );
  }
});

Development

# Install dependencies
npm install

# Build the package
npm run build

# Publish to npm
npm publish

License

MIT

Support

For issues and questions, please visit the GitHub repository.