Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions echo-control/src/app/api/v1/apps/[id]/api-keys/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { getAuthenticatedUser } from '@/lib/auth';
import { logger } from '@/logger';
import { createApiKey, createApiKeySchema } from '@/services/api-keys';
import { NextRequest, NextResponse } from 'next/server';

// POST /api/v1/apps/[id]/api-keys - Create a new API key for the app
export async function POST(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
try {
const { user, echoApp } = await getAuthenticatedUser(request);

const resolvedParams = await params;
const appId = resolvedParams.id;

if (echoApp && echoApp.id !== appId) {
return NextResponse.json({ error: 'Access denied' }, { status: 403 });
}

const body = await request.json().catch(() => ({}));
const parsed = createApiKeySchema.safeParse({
echoAppId: appId,
name: body?.name,
});
if (!parsed.success) {
return NextResponse.json(
{ error: 'Invalid request', details: parsed.error.flatten() },
{ status: 400 }
);
}

const apiKey = await createApiKey(user.id, parsed.data);

logger.emit?.({
severityText: 'INFO',
body: 'API key created',
attributes: { userId: user.id, appId },
});

return NextResponse.json({ apiKey });
} catch (error) {
logger.emit?.({
severityText: 'ERROR',
body: 'Error creating API key',
attributes: {
error: error instanceof Error ? error.message : String(error),
},
});

if (
error instanceof Error &&
(error.message.includes('Not authenticated') ||
error.message.includes('Unauthorized'))
) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}

return NextResponse.json(
{ error: 'Internal server error' },
{ status: 500 }
);
}
}
20 changes: 19 additions & 1 deletion echo-typescript-sdk/src/resources/apps.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { HttpClient } from '../http-client';
import { EchoApp, ListEchoAppsResponse } from '../types';
import { CreateApiKeyResponse, EchoApp, ListEchoAppsResponse } from '../types';
import { BaseResource } from '../utils/error-handling';

export class AppsResource extends BaseResource {
Expand Down Expand Up @@ -34,6 +34,24 @@ export class AppsResource extends BaseResource {
);
}

/**
* Create an API key for the given Echo app
*/
async createApiKey(
appId: string,
name?: string
): Promise<CreateApiKeyResponse> {
return this.handleRequest<CreateApiKeyResponse>(
() =>
this.http.post(`/api/v1/apps/${appId}/api-keys`, {
echoAppId: appId,
...(name && { name }),
}),
'creating API key',
`/api/v1/apps/${appId}/api-keys`
);
}

/**
* Get app URL for a specific Echo app
* @param appId The Echo app ID
Expand Down
Loading