Manage usage plans using the Limitly Next.js SDK
import { Limitly } from '@limitly/limitly-nextjs'; const limitly = new Limitly({ apiKey: process.env.LIMITLY_API_KEY! }); // Create a new plan const newPlan = await limitly.plans.create({ name: 'Basic Plan', description: 'Basic usage plan with 1000 requests per month', max_requests: 1000, request_period: 'month', is_active: true }); console.log('New plan created:', newPlan.data?.id);
// Get all plans const plans = await limitly.plans.list(); console.log('Available plans:', plans.data);
// Get plan details const plan = await limitly.plans.get('plan_123'); console.log('Plan details:', { id: plan.data?.id, name: plan.data?.name, description: plan.data?.description, max_requests: plan.data?.max_requests, request_period: plan.data?.request_period, is_active: plan.data?.is_active });
// Update a plan const updatedPlan = await limitly.plans.update('plan_123', { name: 'Updated Basic Plan', description: 'Updated description', max_requests: 2000, request_period: 'month' }); console.log('Updated plan:', updatedPlan.data);
// Delete a plan await limitly.plans.delete('plan_123'); console.log('Plan deleted successfully');
// Get plan usage const usage = await limitly.plans.getUsage('plan_123'); console.log('Plan usage:', { plan_id: usage.data?.plan_id, plan_name: usage.data?.plan_name, max_requests: usage.data?.max_requests, total_requests: usage.data?.total_requests, percentage_used: usage.data?.percentage_used, users_count: usage.data?.users_count, api_keys_count: usage.data?.api_keys_count });
// Get plan users const users = await limitly.plans.getUsers('plan_123'); console.log('Plan users:', users.data?.users);
// Get plan API keys const apiKeys = await limitly.plans.getKeys('plan_123'); console.log('Plan API keys:', apiKeys.data?.api_keys);
// app/api/plans/route.ts import { Limitly } from '@limitly/limitly-nextjs'; const limitly = new Limitly({ apiKey: process.env.LIMITLY_API_KEY! }); export async function POST(request: Request) { try { const body = await request.json(); const newPlan = await limitly.plans.create({ name: body.name, description: body.description, max_requests: body.max_requests, request_period: body.request_period, is_active: body.is_active }); return Response.json({ success: true, plan: newPlan.data }); } catch (error) { return Response.json({ success: false, error: error.message }, { status: 500 }); } } export async function GET(request: Request) { try { const plans = await limitly.plans.list(); return Response.json({ success: true, plans: plans.data, count: plans.count }); } catch (error) { return Response.json({ success: false, error: error.message }, { status: 500 }); } }
import { Limitly } from '@limitly/limitly-nextjs'; const limitly = new Limitly({ apiKey: process.env.LIMITLY_API_KEY! }); try { const plan = await limitly.plans.create({ name: 'Test Plan', max_requests: 1000, request_period: 'month' }); console.log('Plan created:', plan.data?.id); } catch (error) { if (error.message.includes('Invalid request period')) { console.error('Invalid request period'); } else if (error.message.includes('Plan name exists')) { console.error('Plan name already exists'); } else { console.error('Unexpected error:', error.message); } }
Was this page helpful?