# Sinatra

> Send your first email from a Sinatra app using Net::HTTP against the SendPing API.

Ruby has an official SDK — `gem install sendping` — and it works unchanged inside a Sinatra route: set `SendPing.api_key` once, then call `SendPing::Emails.send({ ... })`. The example below deliberately takes the dependency-free route instead, POSTing JSON to `https://www.sendping.co/api/emails` with the standard-library `net/http`, so there is nothing extra to install beyond Sinatra itself.

## 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. Install Sinatra

Add Sinatra to your project if you have not already.

**RubyGems**

```bash
gem install sinatra
```

## 2. Set your API key

Read your API key from the environment so it never lives in source.

**.env**

```sh
SENDPING_API_KEY=mb_xxxxxxxxx
```

## 3. Send email using HTML

The easiest way to send an email is with the `html` field. POST it to the SendPing API from a route handler.

**index.rb**

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

set :port, 5000
set :bind, "0.0.0.0"

get "/" do
  content_type :json

  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)
  response.body
end
```

> **Note:** A successful call returns the created email's `id`. See [Send an email](https://www.sendping.co/docs/api/emails-send) for all supported fields.
