npm package: @panoptic-it-solutions/autotask-rate-limiter
Description: Rate limiting utilities for Autotask API integration
Latest version: 1.0.19

# Autotask Rate Limiting Utilities

This package provides utilities for rate limiting Autotask API calls using Upstash Redis. It includes:

- Rate limiting middleware for Next.js API routes
- Autotask-specific rate monitoring
- Fetch utilities for making API calls
- Environment variable configuration

## Installation

```bash
npm install @your-org/autotask-rate-limiter
# or
pnpm add @your-org/autotask-rate-limiter
```

## Usage

1. Set up your environment variables:
```bash
cp .env.example .env
# Edit .env with your values
```

2. Use the utilities in your API routes:

```typescript
// app/api/your-route/route.ts
import { NextRequest } from 'next/server';
import { withAutotaskRateLimit } from '@your-org/autotask-rate-limiter';

export async function POST(request: NextRequest) {
  return withAutotaskRateLimit(request, async () => {
    // Your API logic here
  });
}
```

## Features

- Automatic rate limiting using Upstash Redis
- Autotask-specific usage monitoring
- Middleware for Next.js API routes
- TypeScript support
- Configurable rate limits
- Backoff strategy for API calls

## API

### Rate Limiter

```typescript
import { rateLimiter } from '@your-org/autotask-rate-limiter';

// Check rate limit
const { success, limit, remaining } = await rateLimiter.checkRateLimit(identifier);
```

### Autotask Rate Monitor

```typescript
import { checkAutotaskLimits, applyAutotaskBackoff } from '@your-org/autotask-rate-limiter';

// Check Autotask limits
const { success, estimatedUsage } = await checkAutotaskLimits(identifier);

// Apply backoff if needed
await applyAutotaskBackoff();
```

### Fetch Utilities

```typescript
import { fetchAutotaskData } from '@your-org/autotask-rate-limiter';

// Make API calls
const data = await fetchAutotaskData(
  'YourEndpoint/query',
  filter,
  headers
);
```

## Configuration

The package requires the following environment variables:

- `UPSTASH_REDIS_REST_URL`
- `UPSTASH_REDIS_REST_TOKEN`
- `AUTOTASK_API_INTEGRATION_CODE`
- `AUTOTASK_USERNAME`
- `AUTOTASK_PASSWORD`
- `AUTOTASK_API_URL` (Optional): You can optionally provide the full base URL for the Autotask REST API, overriding the zone-based default.

## Prerequisites

To use these utilities within your Next.js project, ensure the following prerequisites are met:

### 1. Environment Variables

The following environment variables must be defined in your project's `.env` file (e.g., `.env`):

- `UPSTASH_REDIS_REST_URL`: The REST URL for your Upstash Redis instance (used for rate limiting).
- `UPSTASH_REDIS_REST_TOKEN`: The access token for your Upstash Redis instance.
- `AUTOTASK_API_INTEGRATION_CODE`: Your Autotask API integration code.
- `AUTOTASK_USERNAME`: The username for Autotask API access.
- `AUTOTASK_SECRET`: The secret/password for Autotask API access.
- `AUTOTASK_API_URL` (Optional): You can optionally provide the full base URL for the Autotask REST API, overriding the zone-based default.

### 2. Required Packages

Ensure the following packages are installed in your main Next.js project's `package.json` dependencies:

```bash
pnpm install @upstash/ratelimit @upstash/redis
# or
npm install @upstash/ratelimit @upstash/redis
# or
yarn add @upstash/ratelimit @upstash/redis
```

**Note:** These utilities were originally developed for a Next.js environment and rely on Next.js types and runtime features (like `NextRequest`, `NextResponse`, and the `fetch` API). Ensure your project environment is compatible.

## Usage

You can now import and use the functions directly from this directory using relative paths or path aliases configured in your `tsconfig.json`.

**Example (`tsconfig.json` path alias):**

```json
// tsconfig.json
{
  "compilerOptions": {
    "paths": {
      "@/autotask-utils/*": ["./autotask-utils/*"]
      // ... other paths
    }
    // ... other options
  }
}
```

**Example Import:**

```typescript
import { checkAutotaskLimits } from "@/autotask-utils/autotask-rate-monitor";
import { fetchAutotaskData } from "@/autotask-utils/fetch";

async function someAction() {
  const rateCheck = await checkAutotaskLimits("some-identifier");
  // ... use utilities
}
```