SDK Reference

TypeScript SDK

Official TypeScript/JavaScript SDK for AMP.

Installation

npm install @amp-protocol/client

Initialisation

import { AMP } from '@amp-protocol/client';

const amp = new AMP({
  apiKey: process.env.AMP_API_KEY,

  // Optional configuration
  baseUrl: 'https://api.amp-protocol.dev/v1',  // Default
  timeout: 5000,                                 // Request timeout (ms)
  retries: 3,                                    // Auto-retry failed requests
  debug: false                                   // Enable debug logging
});

Methods

getContext()

const context = await amp.getContext({
  userId: 'user_123',
  task: 'build a feature',
  complexity: 'medium',      // Optional
  taskType: 'coding',        // Optional
  metadata: { /* ... */ }    // Optional
});

reportOutcome()

await amp.reportOutcome({
  requestId: context.requestId,
  started: true,
  completed: true,
  timeToStart: 45,           // Optional
  flowState: true,           // Optional
  satisfaction: 0.9          // Optional
});

updateProfile()

const profile = await amp.updateProfile({
  userId: 'user_123',
  preferredFraming: 'achievement',
  communicationStyle: 'brief_directive'
});

TypeScript Types

import type {
  GetContextParams,
  MotivationContext,
  OutcomeParams,
  MotivationProfile,
  FramingType,
  CommunicationStyle
} from '@amp-protocol/client';

// Fully typed!
const context: MotivationContext = await amp.getContext({
  userId: 'user_123',
  task: 'test'
});

Error Handling

import { AMPError } from '@amp-protocol/client';

try {
  const context = await amp.getContext({ userId, task });
} catch (error) {
  if (error instanceof AMPError) {
    console.error({
      code: error.code,
      message: error.message,
      statusCode: error.statusCode
    });
  }
}

Event Listeners

// Listen to all requests
amp.on('request', (params) => {
  console.log('AMP Request:', params);
});

// Listen to responses
amp.on('response', (context, duration) => {
  console.log(`AMP responded in ${duration}ms`);
});

// Listen to errors
amp.on('error', (error) => {
  console.error('AMP Error:', error);
});

Framework Examples

Next.js

app/api/agent/route.ts
import { AMP } from '@amp-protocol/client';
import { NextRequest, NextResponse } from 'next/server';

const amp = new AMP({ apiKey: process.env.AMP_API_KEY });

export async function POST(request: NextRequest) {
  const { userId, query } = await request.json();

  const context = await amp.getContext({ userId, task: query });

  return NextResponse.json({ context });
}

Express

server.ts
import { AMP } from '@amp-protocol/client';
import express from 'express';

const amp = new AMP({ apiKey: process.env.AMP_API_KEY });
const app = express();

app.post('/api/agent', async (req, res) => {
  const context = await amp.getContext({
    userId: req.body.userId,
    task: req.body.query
  });

  res.json({ context });
});

💡 Tip: The SDK automatically handles retries and rate limiting. No extra configuration needed!

See Also