# Ruby

> Send your first email from Ruby using Net::HTTP against the SendPing API.

This guide sends email by POSTing JSON to `https://www.sendping.co/api/emails` using the standard-library `net/http`, so there is nothing to install. Want the official gem instead? [Send emails with Ruby](https://www.sendping.co/docs/integrations/ruby-rails) installs `sendping` and calls `SendPing::Emails.send`.

## 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.
- Ruby 2.7 or newer if you install the official `sendping` gem (that is the gemspec floor). The `net/http` example below runs on any supported Ruby.

## 1. Set your API key

**.env**

```sh
SENDPING_API_KEY=mb_xxxxxxxxx
```

## 2. Send email using HTML

The easiest way to send an email is with the `html` field.

**index.rb**

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

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"
# Required: the API answers a missing User-Agent with 403 validation_error.
request["User-Agent"] = "acme-app/1.0"
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)
puts JSON.parse(response.body)["id"]
```

> **Note:** In a Rails or Sinatra app, issue this request from a controller/route — the JSON body is identical. See [Send an email](https://www.sendping.co/docs/api/emails-send) for all fields.
