---
title: Mutations
description: Create, update, and delete records from your frontend using callXrpc.
---
# Mutations
Mutations use `callXrpc` to call hatk's built-in record management endpoints. `callXrpc` is isomorphic, it works in server load functions, Svelte components, and anywhere else you have access to it.
```typescript
// Server-side: in a +layout.server.ts or +page.server.ts
export const load = async () => {
return {
feed: callXrpc("dev.hatk.getFeed", { feed: "recent", limit: 50 }),
}
}
// Client-side: in a Svelte component or query helper
const res = await callXrpc("dev.hatk.createRecord", {
collection: "xyz.statusphere.status" as const,
repo: viewer.did,
record: { status: "🚀", createdAt: new Date().toISOString() },
})
```
Pass SvelteKit's `fetch` as the optional third argument in load functions for request deduplication:
```typescript
callXrpc("dev.hatk.getFeed", { feed: "recent" }, fetch)
```
## Record mutations
hatk generates three built-in procedures for managing records:
| Method | Purpose |
|---|---|
| `dev.hatk.createRecord` | Create a new record in a collection |
| `dev.hatk.deleteRecord` | Delete a record by collection and rkey |
| `dev.hatk.putRecord` | Create or update a record at a specific rkey |
### Creating a record
```typescript
import { callXrpc } from "$hatk/client";
const result = await callXrpc("dev.hatk.createRecord", {
collection: "xyz.statusphere.status" as const,
repo: viewer.did,
record: { status: "🚀", createdAt: new Date().toISOString() },
});
// result.uri — the AT URI of the new record
// result.cid — the content hash
```
The `collection` field uses `as const` so TypeScript narrows the `record` type to match that collection's schema. If your lexicon says `status` is required, you'll get a type error if you omit it.
### Deleting a record
```typescript
await callXrpc("dev.hatk.deleteRecord", {
collection: "xyz.statusphere.status" as const,
rkey: "3abc123",
});
```
The `rkey` is the last segment of the record's AT URI. For example, if the URI is `at://did:plc:abc/xyz.statusphere.status/3abc123`, the rkey is `3abc123`.
### Updating a record
```typescript
await callXrpc("dev.hatk.putRecord", {
collection: "xyz.statusphere.status" as const,
rkey: "3abc123",
record: { status: "☕", createdAt: new Date().toISOString() },
});
```
`putRecord` writes a record at a specific rkey, creating it if it doesn't exist or replacing it if it does.
## Optimistic UI
For a responsive feel, update the UI before the server responds and roll back on failure:
```svelte
```
The same pattern works for deletes -- remove the item from the list immediately, then restore it if the server call fails:
```svelte
```