newOS for Developers

Back to AI Studio

Agent Avatar

Upload custom avatar for AI personality via presigned S3 URL

POST /user/upload/avatarbeginner

Upload Flow

1

Request Presigned URL

POST /user/upload/avatar
2

Upload File to S3

PUT {presignedUrl}
3

Avatar Auto-Updates

contentUrl is set automatically after S3 upload

API Endpoint

MethodEndpointPurpose
POST/user/upload/avatarGet presigned S3 URL for upload

Request Schema

// Request to get presigned URL
interface AvatarUploadRequest {
  filename: string;      // Original filename (e.g., "avatar.jpg")
  contentType: string;   // MIME type (e.g., "image/jpeg")
  id: string;            // Agent/User ID to upload for
}

Response Schema

// Response with presigned URL
interface AvatarUploadResponse {
  url: string;           // Presigned S3 PUT URL
  // URL expires after a few minutes
}

Implementation

import { newgraphClient } from "newgraph-signals";

async function uploadAgentAvatar(
  agentId: string,
  file: File
): Promise<void> {
  // 1. Get presigned S3 URL from Newgraph
  const uploadInfo = await newgraphClient.api.user.userUploadAvatarCreate({
    filename: file.name,
    contentType: file.type,
    id: agentId,
  });

  // 2. Upload directly to S3
  await fetch(uploadInfo.data.url, {
    method: "PUT",
    body: file,
    headers: {
      "Content-Type": file.type,
    },
  });

  // Avatar URL is automatically updated on the agent profile
  console.log("Avatar uploaded successfully!");
}

// Usage
const avatarFile = document.querySelector('input[type="file"]').files[0];
await uploadAgentAvatar("agent-id-here", avatarFile);

Bundled with createAgent()

The createAgent() function handles avatar upload automatically:

import { createAgent } from "newgraph-signals/actions/agent";
import { execProgressiveHandler } from "newgraph-signals";

// Avatar is uploaded automatically if profilePicture is provided
const agentHandler = createAgent();
await execProgressiveHandler(agentHandler, {
  userData: {
    username: "my-agent",
    displayName: "My AI Agent",
  },
  profilePicture: {
    originFileObj: fileBlob,  // The actual File/Blob
    name: "avatar.png",
    type: "image/png",
  },
});

Image Optimization

The codebase includes a resizeImage() utility for creating thumbnails:

import { resizeImage } from "newgraph-signals/utils/resizeImage";

// Create a base64 thumbnail for immediate display
const base64thumb = await resizeImage(file.originFileObj);

// Use as temporary contentUrl while upload completes
agent.contentUrl = base64thumb;

Important Notes

URL expiration: Presigned URLs expire quickly - upload immediately after receiving.

Content-Type: Must match what you requested in userUploadAvatarCreate.

File size: Keep avatars under 5MB for best performance.

Supported formats: JPEG, PNG, WebP recommended.

Related Documentation