Project

treaty

0.0
No release in over a year
A Ruby library for defining and managing REST API contracts with versioning support
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

>= 2.5
>= 13.2
>= 3.13
>= 2.16

Runtime

>= 7.1
>= 2.6
>= 1.14
 Project Readme

Treaty

A Ruby library for defining and managing REST API contracts with versioning support.

Gem Version Release Date Gem Downloads Ruby Version

Warning

Development Status: Treaty is currently under active development in the 0.x version series. Breaking changes may occur between minor versions (0.x) as we refine the API and add new features. The library will stabilize with the 1.0 release. We recommend pinning to specific patch versions in your Gemfile (e.g., gem "treaty", "~> 0.13.0") until the 1.0 release.

📚 Documentation

Explore comprehensive guides and documentation at docs:

💡 Why Treaty?

Treaty provides a complete solution for building versioned APIs in Ruby on Rails:

  • Type Safety - Enforce strict type checking for request and response data
  • API Versioning - Manage multiple concurrent API versions effortlessly
  • Unified Architecture - Request blocks, response blocks, and Entity classes share the same validation system
  • Entity Classes (DTOs) - Define reusable data transfer objects for better code organization
  • Built-in Validation - Validate incoming requests and outgoing responses automatically
  • Data Transformation - Transform data seamlessly between different API versions
  • Inventory System - Pass controller-specific data to services efficiently
  • Deprecation Management - Mark versions as deprecated with flexible conditions
  • Internationalization - Full I18n support for multilingual error messages
  • Well-documented - Comprehensive guides and examples for every feature

🚀 Quick Start

Installation

Add Treaty to your Gemfile:

gem "treaty"

Run:

bundle install

Define Treaty

Create your first API contract in app/treaties/posts/create_treaty.rb:

module Posts
  class CreateTreaty < ApplicationTreaty
    version 1, default: true do
      request do
        object :post do
          string :title
          string :content
          string :summary, :optional
        end
      end

      response 201 do
        object :post do
          string :id
          string :title
          string :content
          string :summary
          datetime :created_at
        end
      end

      delegate_to Posts::CreateService
    end
  end
end

Use in Controller

Define the treaty in your controller app/controllers/posts_controller.rb:

class PostsController < ApplicationController
  # Treaty automatically:
  # 1. Validates incoming parameters according to request definition
  # 2. Calls Posts::CreateService with validated data
  # 3. Validates service response according to response definition
  # 4. Returns transformed data to client
  treaty :create

  # Optional: Provide additional data from controller to service
  treaty :index do
    provide :current_user
    provide :posts, from: :load_posts
  end

  private

  def load_posts
    Post.published.limit(10)
  end
end

🤝 Contributing

We welcome contributions! You can help by:

  • Reporting bugs and suggesting features
  • Writing code and improving documentation
  • Reviewing pull requests
  • Sharing your experience with Treaty

Please read our Contributing Guide before submitting a pull request.

🙏 Acknowledgments

Thank you to all contributors who have helped make Treaty better!

📄 License

Treaty is available as open source under the terms of the MIT License.