JavaScript SDK
Table of Contents
- Installation
- Configuration
- Main Client Class
- Email Functions
- Domain Management
- Webhook Management
- SMTP Configuration
- IMAP Configuration
- Inbound Email Routing
- Type Definitions
Installation
npm install @sparrowengg/sparrow-mailer-sdk@0.1.2-stable.0
Configuration
The SDK requires an API key for authentication. You can initialize the client with the following configuration:
interface SparrowMailConfig {
basePath?: string; // Optional base URL for the API
apiKey: string; // Required API key for authentication
}
Main Client Class
The main client class SparrowMailClient provides access to all SDK functionality:
const client = new SparrowMailClient({
apiKey: "your-api-key",
basePath: "https://api.sparrowmailer.com", // optional
});
Email Functions
sendEmail
async sendEmail(payload: RequestmodelsEmailPayload)
Sends an email using the provided payload.
Parameters:
payload: The email configuration objectfrom: Sender details ({ email: string, name?: string })to: Array of recipients ({ email: string, name?: string }[])subject: Email subjectcontent: Email content ({ type: "text/html" | "text/plain", value: string }[])replyTo(optional): Reply-to email addressbcc(optional): Array of BCC recipientsheaders(optional): Custom headers objectattachments(optional): Array of attachmentssettings(optional): Email settings (tracking, etc.)campaign_id(optional): Campaign identifier
Returns: The response data from the API
getEmailLogs
async getEmailLogs(options?: {
email?: string;
recipientDomain?: string;
refId?: string;
startCreatedAt?: string;
endCreatedAt?: string;
status?: string;
subject?: string;
campaignId?: string;
recipientType?: string;
startDeliveredDate?: string;
endDeliveredDate?: string;
pageSize?: number;
pageToken?: string;
})
Retrieves email logs with optional filtering.
Parameters:
options: Optional filtering parameters
Returns: The email logs response
subscribeEmail
async subscribeEmail(email: string)
Subscribes an email address to EDITH.
Parameters:
email: The email address to subscribe
Returns: The subscription success response
unsubscribeEmail
async unsubscribeEmail(email: string)
Unsubscribes an email address from EDITH.
Parameters:
email: The email address to unsubscribe
Returns: The unsubscription success response
Domain Management
addDomain
async addDomain(payload: RequestmodelsDomainPayload)
Adds a new domain to your account.
Parameters:
payload: The domain configuration objectdomain: The domain name to addcustom_args(optional): Custom arguments objectis_tenant_domain(optional): Whether this is a tenant domainis_tenant_subdomain(optional): Whether this is a tenant subdomainshared_with_subaccounts(optional): specific to subaccount sharingips(optional): Array of IP addressesselector(optional): DKIM selector
Returns: The response data from the API
getDomain
async getDomain(domain: string)
Retrieves details for a specific domain.
Parameters:
domain: The domain name to get details for
Returns: The domain information
verifyDomain
async verifyDomain(domain: string)
Verifies a domain's ownership and configuration.
Parameters:
domain: The domain name to verify
Returns: The verification status
deleteDomain
async deleteDomain(domain: string)
Removes a domain from your account.
Parameters:
domain: The domain name to delete
Returns: The deletion confirmation
Webhook Management
registerWebhook
async registerWebhook(payload: RequestmodelsWebhookCreatePayload)
Registers a new webhook endpoint.
Parameters:
payload: The webhook configuration objectwebhook_url: The URL to receive webhook eventsevent: Event type (e.g.,'EMAIL_EVENT','DOMAIN_VERIFICATION')method: HTTP method ('POST','GET', etc.)headers(optional): Custom headers for the webhook requestwebhook_options(optional): Additional options
Returns: The webhook registration details
updateWebhook
async updateWebhook(payload: RequestmodelsWebhookUpdatePayload)
Updates an existing webhook configuration.
Parameters:
payload: The webhook update configurationwebhook_url(optional): New webhook URLevent(optional): New event typemethod(optional): New HTTP methodheaders(optional): New headerswebhook_options(optional): New options
Returns: The updated webhook details
deleteWebhook
async deleteWebhook(payload: RequestmodelsWebhookDeletePayload)
Removes a webhook configuration.
Returns: The deletion confirmation
SMTP Configuration
configureSMTP
async configureSMTP(payload: RequestmodelsCreateSMTPPayload)
Sets up SMTP configuration.
Parameters:
payload: The SMTP configuration objectname: Configuration nameemail: Associated email addressconfig_type: Must be'smtp'connection_type:'basic'or'oauth'config: SMTP detailshost: SMTP hostport: SMTP portusername: SMTP usernamepassword: SMTP passwordsecure: Security setting ('Auto','Tls','SSL','None')
Returns: The SMTP configuration details
updateSMTP
async updateSMTP(payload: RequestmodelsUpdateSMTPPayload)
Updates existing SMTP configuration.
Parameters:
payload: The SMTP update configurationmailer_id: The mailer ID to updatename: New configuration nameemail: Associated email addressconfig(optional): Updated SMTP details
Returns: The updated SMTP details
getSMTPConfig
async getSMTPConfig(mailerId: string, options?: RawAxiosRequestConfig)
Retrieves SMTP configuration for a specific mailer.
Parameters:
mailerId: The ID of the maileroptions: Optional Axios request configuration
Returns: The SMTP configuration
deleteSMTPConfig
async deleteSMTPConfig(mailerId: string, options?: RawAxiosRequestConfig)
Removes SMTP configuration for a specific mailer.
Parameters:
mailerId: The ID of the maileroptions: Optional Axios request configuration
Returns: The deletion confirmation
IMAP Configuration
configureIMAP
async configureIMAP(payload: RequestmodelsCreateIMAPPayload)
Sets up IMAP configuration.
Parameters:
payload: The IMAP configuration objectname: Configuration nameemail: Associated email addressconfig_type: Must be'imap'connection_type:'basic'or'oauth'config: IMAP detailshost: IMAP hostport: IMAP portusername: IMAP usernamepassword: IMAP passwordsecure: Security setting
webhook(optional): Webhook configuration for IMAP events
Returns: The IMAP configuration details
updateIMAP
async updateIMAP(payload: RequestmodelsUpdateIMAPPayload)
Updates existing IMAP configuration.
Parameters:
payload: The IMAP update configurationmailer_id: The mailer ID to updatename: New configuration nameemail: Associated email addressconfig(optional): Updated IMAP details
Returns: The updated IMAP details
getIMAPConfig
async getIMAPConfig(mailerId: string, options?: RawAxiosRequestConfig)
Retrieves IMAP configuration for a specific mailer.
Parameters:
mailerId: The ID of the maileroptions: Optional Axios request configuration
Returns: The IMAP configuration
deleteIMAPConfig
async deleteIMAPConfig(mailerId: string, options?: RawAxiosRequestConfig)
Removes IMAP configuration for a specific mailer.
Parameters:
mailerId: The ID of the maileroptions: Optional Axios request configuration
Returns: The deletion confirmation
Inbound Email Routing
configureInboundRouting
async configureInboundRouting(payload: RequestmodelsIncomingDomainPayload)
Sets up inbound email routing configuration.
Parameters:
payload: The inbound routing configurationdomain: The domain to configureurl: The URL to forward inbound emails tomethod: HTTP method ('POST', etc.)headers(optional): Custom headers
Returns: The routing configuration details
updateInboundDomain
async updateInboundDomain(domain: string, payload: RequestmodelsUpdateIncomingDomainPayload)
Updates inbound domain configuration.
Parameters:
domain: The domain name to updatepayload: The new configuration payload
Returns: The updated domain configuration
getInboundDomain
async getInboundDomain(domain: string)
Retrieves configuration for an inbound domain.
Parameters:
domain: The domain name to retrieve
Returns: The domain configuration
createInboundUsername
async createInboundUsername(payload: RequestmodelsIncomingEmailPayload)
Creates an inbound username configuration.
Returns: The username configuration details
updateInboundUsername
async updateInboundUsername(
domain: string,
username: string,
payload: RequestmodelsUpdateIncomingEmailPayload
)
Updates an inbound username configuration.
Parameters:
domain: The domain nameusername: The username to updatepayload: The new configuration payload
Returns: The updated username configuration
getInboundUsername
async getInboundUsername(domain: string, username: string)
Retrieves configuration for an inbound username.
Parameters:
domain: The domain nameusername: The username to retrieve
Returns: The username configuration
OAuth Management
setOAuthActive
async setOAuthActive(payload: RequestmodelsOAuthActiveRequest)
Activate or deactivate OAuth configuration.
Parameters:
payload: The activation requestmailer_id: The mailer IDactive(optional): Active status boolean
Returns: The activation status response
oauthLogin
async oauthLogin(payload: RequestmodelsOAuthLoginRequest)
Performs OAuth login.
Parameters:
payload: The OAuth login requestapp_id: Application IDredirect_uri(optional): Redirect URItoken(optional): Token objectmailer_id(optional): Mailer IDtype(optional): Login type
Returns: The login success response
Type Definitions
The SDK exports several type definitions that are used in the function parameters:
export type {
EmailPayload, // Email sending payload
DomainPayload, // Domain configuration payload
WebhookCreatePayload, // Webhook creation payload
CreateSMTPPayload, // SMTP configuration payload
CreateIMAPPayload, // IMAP configuration payload
IncomingDomainPayload, // Inbound domain configuration payload
SMTPConfig, // SMTP configuration type
IMAPConfig, // IMAP configuration type
WebhookConfig, // Webhook configuration type
EmailRecipient, // Email recipient type
Content, // Email content type
Attachment, // Email attachment type
Settings, // General settings type
};
Example Usage
import { SparrowMailClient } from "@sparrowengg/sparrow-mailer-sdk";
// Initialize the client
const client = new SparrowMailClient({
apiKey: "your-api-key",
});
// Send an email
await client.sendEmail({
to: "recipient@example.com",
subject: "Test Email",
content: "Hello, World!",
});
// Add a domain
await client.addDomain({
domain: "example.com",
});
// Configure webhook
await client.registerWebhook({
url: "https://your-webhook-url.com",
events: ["email.sent", "email.delivered"],
});
Features
- Send emails with HTML and plain text content
- Manage email domains
- Configure SMTP settings
- Configure IMAP settings
- Manage webhooks
- Handle inbound email routing
API Reference
Email Operations
sendEmail(payload: EmailPayload): Send an email with full configuration optionsgetEmailLogs(options: object): Retrieve email logs with filteringsubscribeEmail(email: string): Subscribe an email to listsunsubscribeEmail(email: string): Unsubscribe an email from lists
Domain Operations
addDomain(payload: DomainPayload): Add a new domaingetDomain(domain: string): Get domain detailsverifyDomain(domain: string): Verify a domaindeleteDomain(domain: string): Delete a domain
SMTP Operations
configureSMTP(payload: CreateSMTPPayload): Configure SMTP settingsupdateSMTP(payload: UpdateSMTPPayload): Update SMTP configurationgetSMTPConfig(mailerId: string): Get SMTP configurationdeleteSMTPConfig(mailerId: string): Delete SMTP configuration
IMAP Operations
configureIMAP(payload: CreateIMAPPayload): Configure IMAP settingsupdateIMAP(payload: UpdateIMAPPayload): Update IMAP configurationgetIMAPConfig(mailerId: string): Get IMAP configurationdeleteIMAPConfig(mailerId: string): Delete IMAP configuration
Webhook Operations
registerWebhook(payload: WebhookCreatePayload): Register a webhookupdateWebhook(payload: WebhookUpdatePayload): Update webhook settingsdeleteWebhook(payload: WebhookDeletePayload): Delete a webhook
Inbound Email Operations
configureInboundRouting(payload: IncomingDomainPayload): Configure inbound email routingupdateInboundDomain(domain: string, payload: UpdateIncomingDomainPayload): Update inbound domain configurationgetInboundDomain(domain: string): Get inbound domain configurationcreateInboundUsername(payload: IncomingEmailPayload): Create inbound usernameupdateInboundUsername(domain: string, username: string, payload: UpdateIncomingEmailPayload): Update inbound usernamegetInboundUsername(domain: string, username: string): Get inbound username details
OAuth Operations
setOAuthActive(payload: OAuthActiveRequest): Activate/deactivate OAuthoauthLogin(payload: OAuthLoginRequest): Perform OAuth login
Authentication
This SDK uses Bearer token authentication. Your API key is automatically included in all API requests as a Bearer token in the Authorization header.