Keplars

API Examples

Practical code examples for integrating with Keplars Mail Service instant email endpoint

Comprehensive examples for sending instant emails using Keplars Mail Service. All examples use the /instant endpoint for immediate delivery.

Quick Examples

Send Instant Email (Raw Content)

curl -X POST https://api.keplars.com/api/v1/send-email/instant \
  -H "Authorization: Bearer kms_your_api_key.live_..." \
  -H "Content-Type: application/json" \
  -d '{
    "to": ["[email protected]"],
    "subject": "Your verification code",
    "body": "Your verification code is: 123456",
    "is_html": false
  }'

Send Instant Email (HTML Content)

curl -X POST https://api.keplars.com/api/v1/send-email/instant \
  -H "Authorization: Bearer kms_your_api_key.live_..." \
  -H "Content-Type: application/json" \
  -d '{
    "to": ["[email protected]"],
    "subject": "Your verification code",
    "body": "<h1>Verification Code</h1><p>Your code: <strong>123456</strong></p>",
    "is_html": true
  }'

Template-Based Email

curl -X POST https://api.keplars.com/api/v1/send-email/instant \
  -H "Authorization: Bearer kms_your_api_key.live_..." \
  -H "Content-Type: application/json" \
  -d '{
    "to": ["[email protected]"],
    "template_id": "019a1d8f-d961-7ca3-a775-5e7f4e0c6a58",
    "params": {
      "user_name": "John Doe",
      "verification_code": "123456"
    }
  }'

Language Examples

NodeJS

const axios = require('axios');

async function sendInstantEmail(to, subject, body, isHTML = false) {
  try {
    const response = await axios.post(
      'https://api.keplars.com/api/v1/send-email/instant',
      {
        to: [to],
        subject: subject,
        body: body,
        is_html: isHTML
      },
      {
        headers: {
          'Authorization': `Bearer ${process.env.KEPLERS_API_KEY}`,
          'Content-Type': 'application/json'
        }
      }
    );
    
    return response.data;
  } catch (error) {
    throw new Error(`Failed to send email: ${error.response?.data?.message || error.message}`);
  }
}

// Usage
sendInstantEmail(
  '[email protected]',
  'Your verification code',
  'Your code is: 123456'
);

Python

import requests

def send_instant_email(to, subject, body, is_html=False):
    try:
        response = requests.post(
            'https://api.keplars.com/api/v1/send-email/instant',
            json={
                'to': [to],
                'subject': subject,
                'body': body,
                'is_html': is_html
            },
            headers={
                'Authorization': f'Bearer {os.getenv("KEPLERS_API_KEY")}',
                'Content-Type': 'application/json'
            }
        )
        response.raise_for_status()
        return response.json()
    except requests.exceptions.RequestException as e:
        raise Exception(f'Failed to send email: {str(e)}')

# Usage
send_instant_email(
    '[email protected]',
    'Your verification code',
    'Your code is: 123456'
)

Go

package main

import (
    "bytes"
    "encoding/json"
    "net/http"
    "os"
)

type EmailRequest struct {
    To      []string `json:"to"`
    Subject string   `json:"subject"`
    Body    string   `json:"body"`
    IsHTML  bool     `json:"is_html"`
}

func sendInstantEmail(to, subject, body string, isHTML bool) error {
    email := EmailRequest{
        To:      []string{to},
        Subject: subject,
        Body:    body,
        IsHTML:  isHTML,
    }
    
    jsonData, err := json.Marshal(email)
    if err != nil {
        return err
    }
    
    req, err := http.NewRequest("POST", "https://api.keplars.com/api/v1/send-email/instant", bytes.NewBuffer(jsonData))
    if err != nil {
        return err
    }
    
    req.Header.Set("Authorization", "Bearer "+os.Getenv("KEPLERS_API_KEY"))
    req.Header.Set("Content-Type", "application/json")
    
    client := &http.Client{}
    resp, err := client.Do(req)
    if err != nil {
        return err
    }
    defer resp.Body.Close()
    
    return nil
}

// Usage
func main() {
    sendInstantEmail(
        "[email protected]",
        "Your verification code",
        "Your code is: 123456",
        false,
    )
}

For more detailed language implementations, see the Languages section.

On this page