# Ruby on Rails

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

Ruby has an official gem — `gem install sendping` — and it works from any Rails controller or job. The example below shows the dependency-free alternative instead: POST JSON to `https://www.sendping.co/api/emails` with the standard-library `net/http`, so there is nothing to install. See [SDKs](https://www.sendping.co/docs/resources/sdks) for the gem.

## Prerequisites

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

## 1. Add your API key

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

**.env**

```ini
SENDPING_API_KEY=mb_xxxxxxxxx
```

## 2. Send email from a controller

POST the email body to the SendPing 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://www.sendping.co/api/emails")

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

    request = Net::HTTP::Post.new(uri)
    request["Authorization"] = "Bearer #{ENV.fetch('SENDPING_API_KEY')}"
    request["Content-Type"] = "application/json"
    request.body = {
      from: "Acme <onboarding@yourdomain.com>",
      to: ["delivered@test.sendping.co"],
      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.sendping.co/docs/api/emails-send) reference.
