# Ruby on Rails

> Send your first email from a Rails controller by calling the MailBlastr API with Net::HTTP.

Send your first email from a Rails app by calling the MailBlastr REST API directly. The `mailblastr` SDK is for Node.js — Ruby users POST JSON to `https://api.mailblastr.com/emails` using the standard-library `net/http`, so there is nothing to install.

## Prerequisites

- A MailBlastr [API key](https://www.mailblastr.com/docs/api-keys/overview).
- A [verified domain](https://www.mailblastr.com/docs/domains/managing) to send from.

## 1. Add your API key

Store your key in the environment (or Rails credentials). The example reads `MAILBLASTR_API_KEY` from `ENV`.

**.env**

```ini
MAILBLASTR_API_KEY=mb_xxxxxxxxx
```

## 2. Send email from a controller

POST the email body to the MailBlastr API from a controller action with `net/http`. The easiest way to send is with the `html` field.

**app/controllers/send_controller.rb**

```rb
require "net/http"
require "json"
require "uri"

class SendController < ApplicationController
  def create
    uri = URI("https://api.mailblastr.com/emails")

    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true

    request = Net::HTTP::Post.new(uri)
    request["Authorization"] = "Bearer #{ENV.fetch('MAILBLASTR_API_KEY')}"
    request["Content-Type"] = "application/json"
    request.body = {
      from: "Acme <onboarding@yourdomain.com>",
      to: ["delivered@example.com"],
      subject: "hello world",
      html: "<strong>it works!</strong>",
    }.to_json

    response = http.request(request)
    render json: JSON.parse(response.body), status: response.code.to_i
  end
end
```

> **Note:** A successful call returns the created email's `id`. See every supported field in the [Send an email](https://www.mailblastr.com/docs/api/emails-send) reference.
