TypeScript SDK for Plasmic Admin API
npm install @elasticpath/plasmic-backend-sdkTypeScript SDK for Plasmic Admin API with automatic authentication handling.
- Type-safe API client generated from OpenAPI specification
- Automatic CSRF token management
- Session-based cookie authentication
- Support for all Plasmic project, workspace, and admin operations
``bash`
pnpm add @elasticpath/plasmic-backend-sdk
`typescript
import { createPlasmicClient, listProjects, createProject } from '@elasticpath/plasmic-backend-sdk';
// Create an authenticated client
const plasmic = createPlasmicClient('https://studio.plasmic.app');
// Authenticate
await plasmic.authenticate('user@example.com', 'password');
// List all projects
const { data } = await listProjects({
client: plasmic.client,
query: { query: 'all' },
});
console.log(data?.projects);
`
The SDK handles session-based authentication with CSRF token management:
`typescript
const plasmic = createPlasmicClient({
baseUrl: 'https://studio.plasmic.app',
throwOnError: true, // default: true
});
// Login with email/password
await plasmic.authenticate('user@example.com', 'password');
// Check if authenticated
console.log(plasmic.isAuthenticated); // true
// Sign out
await plasmic.signOut();
`
`typescript`
// Check if an existing session is still valid
const isValid = await plasmic.checkSession();
if (!isValid) {
await plasmic.authenticate('user@example.com', 'password');
}
`typescript
import {
listProjects,
getProject,
createProject,
updateProject,
deleteProject,
cloneProject,
} from '@elasticpath/plasmic-backend-sdk';
// List all projects
const { data } = await listProjects({
client: plasmic.client,
query: { query: 'all' },
});
// Get a specific project
const { data: project } = await getProject({
client: plasmic.client,
path: { projectId: 'project-id' },
});
// Create a new project
const { data: newProject } = await createProject({
client: plasmic.client,
body: {
name: 'My New Project',
workspaceId: 'workspace-id',
},
});
// Update a project
await updateProject({
client: plasmic.client,
path: { projectId: 'project-id' },
body: { name: 'Updated Name' },
});
// Delete a project
await deleteProject({
client: plasmic.client,
path: { projectId: 'project-id' },
});
// Clone a project
const { data: cloned } = await cloneProject({
client: plasmic.client,
path: { projectId: 'project-id' },
body: {
name: 'Copy of Project',
workspaceId: 'target-workspace-id',
},
});
`
`typescript
import {
listWorkspaces,
getWorkspace,
createWorkspace,
updateWorkspace,
deleteWorkspace,
} from '@elasticpath/plasmic-backend-sdk';
// List all workspaces
const { data } = await listWorkspaces({ client: plasmic.client });
// Get a specific workspace
const { data: workspace } = await getWorkspace({
client: plasmic.client,
path: { workspaceId: 'workspace-id' },
});
// Create a new workspace
const { data: newWorkspace } = await createWorkspace({
client: plasmic.client,
body: {
name: 'My Workspace',
teamId: 'team-id',
},
});
`
Admin operations require the user to be listed in the server's admin email configuration.
`typescript
import {
adminListProjects,
adminCloneProject,
adminDeleteProject,
adminRestoreProject,
adminChangeProjectOwner,
adminRevertRevision,
} from '@elasticpath/plasmic-backend-sdk';
// List all projects (admin)
const { data } = await adminListProjects({
client: plasmic.client,
body: { ownerId: 'user-id' }, // optional filter
});
// Clone any project (admin)
const { data: cloned } = await adminCloneProject({
client: plasmic.client,
body: {
projectId: 'project-id',
revisionNum: 5, // optional: clone from specific revision
},
});
// Restore a deleted project
await adminRestoreProject({
client: plasmic.client,
body: { id: 'project-id' },
});
// Change project owner
await adminChangeProjectOwner({
client: plasmic.client,
body: {
projectId: 'project-id',
ownerEmail: 'newowner@example.com',
},
});
// Revert to a previous revision
await adminRevertRevision({
client: plasmic.client,
body: {
projectId: 'project-id',
revision: 10,
},
});
`
`bashInstall dependencies
pnpm install
$3
If the OpenAPI specification changes, regenerate the SDK:
`bash
pnpm generate
``This uses heyapi to generate type-safe API functions from the OpenAPI spec.
MIT