# Sinatra

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

The `mailblastr` SDK is for Node.js — Ruby users send email by POSTing JSON to `https://api.mailblastr.com/emails`. The example below uses the standard-library `net/http` from inside a Sinatra route, so there is nothing extra to install beyond Sinatra itself.

## 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. 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
MAILBLASTR_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 MailBlastr 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://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)
  response.body
end
```

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