Skip to main content

Send Email

import { OneShot } from '@oneshot/sdk';

const agent = new OneShot({
  privateKey: process.env.AGENT_PRIVATE_KEY
});

// Simple email
await agent.email({
  to: '[email protected]',
  subject: 'Hello from OneShot',
  body: 'This email was sent autonomously!'
});

// Email with attachments
await agent.email({
  to: '[email protected]',
  subject: 'Report attached',
  body: 'Please find the report attached.',
  attachments: [
    {
      filename: 'report.pdf',
      content: base64Content,
      content_type: 'application/pdf'
    }
  ]
});

Bulk Email

Send the same email to multiple recipients:
import { OneShot } from '@oneshot/sdk';

const agent = new OneShot({
  privateKey: process.env.AGENT_PRIVATE_KEY
});

// Send to multiple recipients
await agent.email({
  to: [
    '[email protected]',
    '[email protected]',
    '[email protected]'
  ],
  subject: 'Weekly Team Update',
  body: 'Here is this week\'s progress report...'
});

// Each recipient gets an individual email (not CC/BCC)
The SDK automatically handles bulk quoting and pricing. Service fees scale with the number of recipients.

Deep Research

// Quick research (5-10 sources, 2-3 minutes)
const quickReport = await agent.research({
  topic: 'Latest developments in AI agents',
  depth: 'quick'
});

console.log(quickReport.report_content);
console.log(`Sources: ${quickReport.sources_count}`);

// Comprehensive research (20+ sources, 5-10 minutes)
const deepReport = await agent.research({
  topic: 'Blockchain scalability solutions',
  depth: 'deep'
});

console.log(deepReport.report_content);
console.log(`Report saved to: ${deepReport.report_gcs_uri}`);

Poll Inbox

// The SDK doesn't have a built-in inbox method yet
// Use the universal tool method:

const inbox = await agent.tool('inbox', {
  include_body: true,
  limit: 10
});

for (const email of inbox.emails) {
  console.log(`From: ${email.from}`);
  console.log(`Subject: ${email.subject}`);
  console.log(`Body: ${email.body}`);
  
  // Process attachments
  if (email.attachments) {
    for (const att of email.attachments) {
      const buffer = Buffer.from(att.content, 'base64');
      // Process attachment...
    }
  }
}

Universal Tool Method

Call any OneShot tool using the universal tool() method:
// Email quote
const quote = await agent.tool('email/quote', {
  from_address: '[email protected]',
  to_address: '[email protected]',
  subject: 'Test',
  body: 'Hello'
});

console.log(`Cost: $${quote.total_cost}`);

// Email send (with quote ID)
const send = await agent.toolWithQuote('email/send', {
  from_address: '[email protected]',
  to_address: '[email protected]',
  subject: 'Test',
  body: 'Hello'
}, quote.quote_id);

Check Wallet Balance

const agent = new OneShot({
  privateKey: process.env.AGENT_PRIVATE_KEY
});

console.log('Agent address:', agent.address);

const balance = await agent.getBalance();
console.log(`Wallet balance: ${balance}`);

if (parseFloat(balance) < 1.0) {
  console.warn('Low balance! Please fund your wallet.');
}

Error Handling

try {
  await agent.email({
    to: '[email protected]',
    subject: 'Test',
    body: 'Hello'
  });
} catch (error) {
  if (error.message.includes('Insufficient balance')) {
    console.error('Not enough funds in wallet');
  } else if (error.message.includes('domain_unavailable')) {
    console.error('Domain is not available');
  } else {
    console.error('Error:', error.message);
  }
}

Complete Agent Example

import { OneShot } from '@oneshot/sdk';

class EmailAgent {
  private agent: OneShot;
  private lastCheck: string;

  constructor(privateKey: string) {
    this.agent = new OneShot({ privateKey });
    this.lastCheck = new Date().toISOString();
  }

  async start() {
    console.log(`🤖 Agent started: ${this.agent.address}`);
    
    // Poll inbox every minute
    setInterval(() => this.pollInbox(), 60000);
  }

  async pollInbox() {
    try {
      const inbox = await this.agent.tool('inbox', {
        since: this.lastCheck,
        include_body: true,
        limit: 50
      });

      if (inbox.count > 0) {
        console.log(`📧 Received ${inbox.count} new email(s)`);
        
        for (const email of inbox.emails) {
          await this.processEmail(email);
        }
        
        this.lastCheck = new Date().toISOString();
      }
    } catch (error) {
      console.error('Error polling inbox:', error);
    }
  }

  async processEmail(email: any) {
    console.log(`Processing email from ${email.from}`);
    
    // Generate AI response
    const reply = await this.generateReply(email);
    
    // Send reply
    await this.agent.email({
      to: email.from,
      subject: `Re: ${email.subject}`,
      body: reply
    });
    
    console.log(`✅ Replied to ${email.from}`);
  }

  async generateReply(email: any): Promise<string> {
    // Use your LLM here to generate a reply
    return `Thank you for your email. I'll get back to you soon!`;
  }
}

// Start the agent
const agent = new EmailAgent(process.env.AGENT_PRIVATE_KEY!);
agent.start();

Next Steps