Skip to content

oryono/africastalking-elixir

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

9 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

AfricastalkingElixir

Hex.pm Documentation

Elixir client library for Africa's Talking API

A simple, robust Elixir library for integrating with Africa's Talking services. This library provides easy-to-use functions for sending SMS messages and airtime transfers.

Features

  • πŸ“± SMS Messaging - Send SMS to single or multiple recipients
  • πŸ’° Airtime Transfer - Send airtime to multiple recipients with different currencies
  • πŸ”’ Secure - Proper API key handling and validation
  • ⚑ Fast - Built with Hackney for efficient HTTP requests
  • πŸ§ͺ Well Tested - Comprehensive test coverage
  • πŸ“– Well Documented - Complete documentation with examples

Installation

Add africastalking_elixir to your list of dependencies in mix.exs:

def deps do
  [
    {:africastalking_elixir, "~> 0.1.0"}
  ]
end

Then run:

$ mix deps.get

Configuration

Configure your Africa's Talking credentials in config/config.exs:

config :africastalking_elixir,
  username: "your_username",
  api_key: "your_api_key",
  base_url: "https://api.africastalking.com"  # Use sandbox URL for testing

Using Environment Variables

For production environments, you can use environment variables:

config :africastalking_elixir,
  username: {:system, "AFRICASTALKING_USERNAME"},
  api_key: {:system, "AFRICASTALKING_API_KEY"},
  base_url: {:system, "AFRICASTALKING_BASE_URL"}

Sandbox vs Production

  • Sandbox: https://api.sandbox.africastalking.com (for testing)
  • Production: https://api.africastalking.com (for live transactions)

Quick Start

Sending SMS

# Send SMS to a single recipient
{:ok, response} = AfricastalkingElixir.send_sms("+254722000000", "Hello World!")

# Send SMS to multiple recipients
{:ok, response} = AfricastalkingElixir.send_sms(
  "+254722000000,+254733000000,+254744000000",
  "Hello everyone!"
)

Sending Airtime

# Send airtime to single recipient
recipients = [
  %{
    phone_number: "+254722000000",
    amount: 100,
    currency_code: "KES"
  }
]

{:ok, response} = AfricastalkingElixir.send_airtime(recipients)

# Send airtime to multiple recipients with different amounts
recipients = [
  %{phone_number: "+254722000000", amount: 100, currency_code: "KES"},
  %{phone_number: "+254733000000", amount: 50, currency_code: "KES"},
  %{phone_number: "+234803000000", amount: 200, currency_code: "NGN"}
]

{:ok, response} = AfricastalkingElixir.send_airtime(recipients)

Usage Examples

SMS Examples

# Basic SMS
case AfricastalkingElixir.send_sms("+254722000000", "Hello World") do
  {:ok, %{status_code: 201, africastalking_response: response}} ->
    IO.puts("SMS sent successfully!")
    IO.inspect(response)

  {:error, reason} ->
    IO.puts("Failed to send SMS: #{reason}")
end

# SMS to multiple recipients
phones = "+254722000000,+254733000000"
message = "Meeting at 3 PM today. Please confirm."

{:ok, response} = AfricastalkingElixir.send_sms(phones, message)

Airtime Examples

# Single airtime transfer
recipients = [
  %{
    phone_number: "+254722000000",
    amount: 100,
    currency_code: "KES"
  }
]

case AfricastalkingElixir.send_airtime(recipients) do
  {:ok, %{status_code: 201, africastalking_response: response}} ->
    IO.puts("Airtime sent successfully!")
    IO.inspect(response)

  {:error, reason} ->
    IO.puts("Failed to send airtime: #{reason}")
end

# Bulk airtime transfer
recipients = [
  %{phone_number: "+254722000000", amount: 100, currency_code: "KES"},
  %{phone_number: "+254733000000", amount: 50, currency_code: "KES"},
  %{phone_number: "+234803000000", amount: 200, currency_code: "NGN"},
  %{phone_number: "+233240000000", amount: 5, currency_code: "GHS"}
]

{:ok, response} = AfricastalkingElixir.send_airtime(recipients)

Supported Countries and Currencies

SMS

SMS is supported in all Africa's Talking supported countries.

Airtime

Common supported currencies include:

  • KES - Kenyan Shilling
  • UGX - Ugandan Shilling
  • TZS - Tanzanian Shilling
  • RWF - Rwandan Franc
  • ZMW - Zambian Kwacha
  • NGN - Nigerian Naira
  • GHS - Ghanaian Cedi
  • XOF - West African CFA Franc
  • XAF - Central African CFA Franc
  • ETB - Ethiopian Birr
  • MWK - Malawian Kwacha
  • USD - US Dollar

Response Format

SMS Response

{:ok, %{
  status_code: 201,
  africastalking_response: %{
    "SMSMessageData" => %{
      "Message" => "Sent to 1/1 Total Cost: KES 0.8000",
      "Recipients" => [
        %{
          "cost" => "KES 0.8000",
          "messageId" => "ATXid_sample123",
          "number" => "+254722000000",
          "status" => "Success",
          "statusCode" => 101
        }
      ]
    }
  }
}}

Airtime Response

{:ok, %{
  status_code: 201,
  africastalking_response: %{
    "entries" => [
      %{
        "phoneNumber" => "+254722000000",
        "amount" => "KES 100.0000",
        "status" => "Sent",
        "requestId" => "ATQid_sample123",
        "discount" => "KES 2.0000"
      }
    ],
    "numQueued" => 1,
    "totalAmount" => "KES 100.0000",
    "totalDiscount" => "KES 2.0000"
  }
}}

Error Handling

The library provides detailed error messages for various scenarios:

# Invalid phone number
{:error, "One of the phone numbers is invalid, Please check and try again"}

# Missing recipient
{:error, "Recipient is required. Please provide a string number or a comma separated list of string phone numbers"}

# Invalid currency code
{:error, "One of the currency codes does not conform to the 3-digit ISO standard"}

# Missing required fields for airtime
{:error, "Phone number, currency code and amount must be specified for each recipient"}

# Network or API errors
{:error, "timeout"}
{:error, "connection_failed"}

Phone Number Format

Phone numbers should be in international format:

  • βœ… "+254722000000" (with country code)
  • βœ… "254722000000" (without + sign)
  • βœ… "0722000000" (local format, will be validated)
  • ❌ "722000000" (missing country/area code)

Testing

Run the test suite:

$ mix test

For testing with the sandbox environment, make sure to use the sandbox URL in your configuration:

config :africastalking_elixir,
  username: "sandbox",
  api_key: "your_sandbox_api_key",
  base_url: "https://api.sandbox.africastalking.com"

Documentation

Full documentation is available on HexDocs.

You can also generate documentation locally:

$ mix docs

Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Support

Related Projects

About

Africa's Talking SDK for Elixir

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages