[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"project-7649":3},{"id":4,"name":5,"fullName":6,"owner":7,"repo":5,"description":8,"homepage":9,"htmlUrl":10,"language":11,"languages":10,"totalLinesOfCode":10,"stars":12,"forks":13,"watchers":14,"openIssues":15,"contributorsCount":16,"subscribersCount":16,"size":16,"stars1d":17,"stars7d":18,"stars30d":19,"stars90d":16,"forks30d":16,"starsTrendScore":20,"compositeScore":21,"rankGlobal":10,"rankLanguage":10,"license":22,"archived":23,"fork":23,"defaultBranch":24,"hasWiki":23,"hasPages":23,"topics":25,"createdAt":10,"pushedAt":10,"updatedAt":29,"readmeContent":30,"aiSummary":31,"trendingCount":16,"starSnapshotCount":16,"syncStatus":18,"lastSyncTime":32,"discoverSource":33},7649,"grape","ruby-grape\u002Fgrape","ruby-grape","An opinionated framework for creating REST-like APIs in Ruby.","http:\u002F\u002Fwww.ruby-grape.org",null,"Ruby",9987,1230,194,214,0,1,2,5,3,40.27,"MIT License",false,"master",[26,5,27,28],"api","hacktoberfest","ruby","2026-06-12 02:01:42","![grape logo](grape.png)\n\n[![Gem Version](https:\u002F\u002Fbadge.fury.io\u002Frb\u002Fgrape.svg)](http:\u002F\u002Fbadge.fury.io\u002Frb\u002Fgrape)\n[![test](https:\u002F\u002Fgithub.com\u002Fruby-grape\u002Fgrape\u002Factions\u002Fworkflows\u002Ftest.yml\u002Fbadge.svg)](https:\u002F\u002Fgithub.com\u002Fruby-grape\u002Fgrape\u002Factions\u002Fworkflows\u002Ftest.yml)\n[![Coverage Status](https:\u002F\u002Fcoveralls.io\u002Frepos\u002Fgithub\u002Fruby-grape\u002Fgrape\u002Fbadge.svg?branch=master)](https:\u002F\u002Fcoveralls.io\u002Fgithub\u002Fruby-grape\u002Fgrape?branch=master)\n\n## What is Grape?\n\nGrape is a REST-like API framework for Ruby. It's designed to run on Rack or complement existing web application frameworks such as Rails and Sinatra by providing a simple DSL to easily develop RESTful APIs. It has built-in support for common conventions, including multiple formats, subdomain\u002Fprefix restriction, content negotiation, versioning and much more.\n\n## Stable Release\n\nYou're reading the documentation for the next release of Grape, which should be 3.3.0.\nThe current stable release is [3.2.0](https:\u002F\u002Fgithub.com\u002Fruby-grape\u002Fgrape\u002Fblob\u002Fv3.2.0\u002FREADME.md).\n\n## Project Resources\n\n* [Grape Website](http:\u002F\u002Fwww.ruby-grape.org)\n* [Documentation](http:\u002F\u002Fwww.rubydoc.info\u002Fgems\u002Fgrape)\n* Need help? [Open an Issue](https:\u002F\u002Fgithub.com\u002Fruby-grape\u002Fgrape\u002Fissues)\n* [Follow us on Twitter](https:\u002F\u002Ftwitter.com\u002Fgrapeframework)\n\n## Grape for Enterprise\n\nAvailable as part of the Tidelift Subscription.\n\nThe maintainers of Grape are working with Tidelift to deliver commercial support and maintenance. Save time, reduce risk, and improve code health, while paying the maintainers of Grape. Click [here](https:\u002F\u002Ftidelift.com\u002Fsubscription\u002Frequest-a-demo?utm_source=rubygems-grape&utm_medium=referral&utm_campaign=enterprise) for more details.\n\n## Installation\n\nRuby 3.2 or newer is required.\n\nGrape is available as a gem, to install it run:\n\n    bundle add grape\n\n## Basic Usage\n\nGrape APIs are Rack applications that are created by subclassing `Grape::API`.\nBelow is a simple example showing some of the more common features of Grape in the context of recreating parts of the Twitter API.\n\n```ruby\nmodule Twitter\n  class API \u003C Grape::API\n    version 'v1', using: :header, vendor: 'twitter'\n    format :json\n    prefix :api\n\n    helpers do\n      def current_user\n        @current_user ||= User.authorize!(env)\n      end\n\n      def authenticate!\n        error!('401 Unauthorized', 401) unless current_user\n      end\n    end\n\n    resource :statuses do\n      desc 'Return a public timeline.'\n      get :public_timeline do\n        Status.limit(20)\n      end\n\n      desc 'Return a personal timeline.'\n      get :home_timeline do\n        authenticate!\n        current_user.statuses.limit(20)\n      end\n\n      desc 'Return a status.'\n      params do\n        requires :id, type: Integer, desc: 'Status ID.'\n      end\n      route_param :id do\n        get do\n          Status.find(params[:id])\n        end\n      end\n\n      desc 'Create a status.'\n      params do\n        requires :status, type: String, desc: 'Your status.'\n      end\n      post do\n        authenticate!\n        Status.create!({\n          user: current_user,\n          text: params[:status]\n        })\n      end\n\n      desc 'Update a status.'\n      params do\n        requires :id, type: String, desc: 'Status ID.'\n        requires :status, type: String, desc: 'Your status.'\n      end\n      put ':id' do\n        authenticate!\n        current_user.statuses.find(params[:id]).update({\n          user: current_user,\n          text: params[:status]\n        })\n      end\n\n      desc 'Delete a status.'\n      params do\n        requires :id, type: String, desc: 'Status ID.'\n      end\n      delete ':id' do\n        authenticate!\n        current_user.statuses.find(params[:id]).destroy\n      end\n    end\n  end\nend\n```\n\n## Rails 7.1\n\nGrape's [deprecator](https:\u002F\u002Fapi.rubyonrails.org\u002Fv7.1.0\u002Fclasses\u002FActiveSupport\u002FDeprecation.html) will be added to your application's deprecators [automatically](lib\u002Fgrape\u002Frailtie.rb) as `:grape`, so that your application's configuration can be applied to it.\n\n## Mounting\n\n### All\n\n\nBy default Grape will compile the routes on the first route, but it is possible to pre-load routes using the `compile!` method.\n\n```ruby\nTwitter::API.compile!\n```\n\nThis can be added to your `config.ru` (if using rackup), `application.rb` (if using rails), or any file that loads your server.\n\n### Rack\n\nThe above sample creates a Rack application that can be run from a rackup `config.ru` file with `rackup`:\n\n```ruby\nrun Twitter::API\n```\n\n(With pre-loading you can use)\n\n```ruby\nTwitter::API.compile!\nrun Twitter::API\n```\n\nAnd would respond to the following routes:\n\n    GET \u002Fapi\u002Fstatuses\u002Fpublic_timeline\n    GET \u002Fapi\u002Fstatuses\u002Fhome_timeline\n    GET \u002Fapi\u002Fstatuses\u002F:id\n    POST \u002Fapi\u002Fstatuses\n    PUT \u002Fapi\u002Fstatuses\u002F:id\n    DELETE \u002Fapi\u002Fstatuses\u002F:id\n\nGrape will also automatically respond to HEAD and OPTIONS for all GET, and just OPTIONS for all other routes.\n\n### Alongside Sinatra (or other frameworks)\n\nIf you wish to mount Grape alongside another Rack framework such as Sinatra, you can do so easily using `Rack::Cascade`:\n\n```ruby\n# Example config.ru\n\nrequire 'sinatra'\nrequire 'grape'\n\nclass API \u003C Grape::API\n  get :hello do\n    { hello: 'world' }\n  end\nend\n\nclass Web \u003C Sinatra::Base\n  get '\u002F' do\n    'Hello world.'\n  end\nend\n\nuse Rack::Session::Cookie\nrun Rack::Cascade.new [Web, API]\n```\n\nNote that order of loading apps using `Rack::Cascade` matters. The grape application must be last if you want to raise custom 404 errors from grape (such as `error!('Not Found',404)`). If the grape application is not last and returns 404 or 405 response, [cascade utilizes that as a signal to try the next app](https:\u002F\u002Fwww.rubydoc.info\u002Fgems\u002Frack\u002FRack\u002FCascade). This may lead to undesirable behavior showing the [wrong 404 page from the wrong app](https:\u002F\u002Fgithub.com\u002Fruby-grape\u002Fgrape\u002Fissues\u002F1515).\n\n\n### Rails\n\nPlace API files into `app\u002Fapi`. Rails expects a subdirectory that matches the name of the Ruby module and a file name that matches the name of the class. In our example, the file name location and directory for `Twitter::API` should be `app\u002Fapi\u002Ftwitter\u002Fapi.rb`.\n\nModify `config\u002Froutes`:\n\n```ruby\nmount Twitter::API => '\u002F'\n```\n#### Zeitwerk\nRails's default autoloader is `Zeitwerk`. By default, it inflects `api` as `Api` instead of `API`. To make our example work, you need to uncomment the lines at the bottom of `config\u002Finitializers\u002Finflections.rb`, and add `API` as an acronym:\n\n```ruby\nActiveSupport::Inflector.inflections(:en) do |inflect|\n  inflect.acronym 'API'\nend\n```\n\n### Modules\n\nYou can mount multiple API implementations inside another one. These don't have to be different versions, but may be components of the same API.\n\n```ruby\nclass Twitter::API \u003C Grape::API\n  mount Twitter::APIv1\n  mount Twitter::APIv2\nend\n```\n\nYou can also mount on a path, which is similar to using `prefix` inside the mounted API itself.\n\n```ruby\nclass Twitter::API \u003C Grape::API\n  mount Twitter::APIv1 => '\u002Fv1'\nend\n```\n\nDeclarations as `before\u002Fafter\u002Frescue_from` can be placed before or after `mount`. In any case they will be inherited.\n\n```ruby\nclass Twitter::API \u003C Grape::API\n  before do\n    header 'X-Base-Header', 'will be defined for all APIs that are mounted below'\n  end\n\n  rescue_from :all do\n    error!({ \"error\" => \"Internal Server Error\" }, 500)\n  end\n\n  mount Twitter::Users\n  mount Twitter::Search\n\n  after do\n    clean_cache!\n  end\n\n  rescue_from ZeroDivisionError do\n    error!({ \"error\" => \"Not found\" }, 404)\n  end\nend\n```\n\n## Remounting\n\nYou can mount the same endpoints in two different locations.\n\n```ruby\nclass Voting::API \u003C Grape::API\n  namespace 'votes' do\n    get do\n      # Your logic\n    end\n\n    post do\n      # Your logic\n    end\n  end\nend\n\nclass Post::API \u003C Grape::API\n  mount Voting::API\nend\n\nclass Comment::API \u003C Grape::API\n  mount Voting::API\nend\n```\n\nAssuming that the post and comment endpoints are mounted in `\u002Fposts` and `\u002Fcomments`, you should now be able to do `get \u002Fposts\u002Fvotes`, `post \u002Fposts\u002Fvotes`, `get \u002Fcomments\u002Fvotes` and `post \u002Fcomments\u002Fvotes`.\n\n### Mount Configuration\n\nYou can configure remountable endpoints to change how they behave according to where they are mounted.\n\n```ruby\nclass Voting::API \u003C Grape::API\n  namespace 'votes' do\n    desc \"Vote for your #{configuration[:votable]}\"\n    get do\n      # Your logic\n    end\n  end\nend\n\nclass Post::API \u003C Grape::API\n  mount Voting::API, with: { votable: 'posts' }\nend\n\nclass Comment::API \u003C Grape::API\n  mount Voting::API, with: { votable: 'comments' }\nend\n```\n\nNote that if you're passing a hash as the first parameter to `mount`, you will need to explicitly put `()` around parameters:\n```ruby\n# good\nmount({ ::Some::Api => '\u002Fsome\u002Fapi' }, with: { condition: true })\n\n# bad\nmount ::Some::Api => '\u002Fsome\u002Fapi', with: { condition: true }\n```\n\nYou can access `configuration` on the class (to use as dynamic attributes), inside blocks (like namespace)\n\nIf you want logic happening given on an `configuration`, you can use the helper `given`.\n\n```ruby\nclass ConditionalEndpoint::API \u003C Grape::API\n  given configuration[:some_setting] do\n    get 'mount_this_endpoint_conditionally' do\n      configuration[:configurable_response]\n    end\n  end\nend\n```\n\nIf you want a block of logic running every time an endpoint is mounted (within which you can access the `configuration` Hash)\n\n\n```ruby\nclass ConditionalEndpoint::API \u003C Grape::API\n  mounted do\n    YourLogger.info \"This API was mounted at: #{Time.now}\"\n\n    get configuration[:endpoint_name] do\n      configuration[:configurable_response]\n    end\n  end\nend\n```\n\nMore complex results can be achieved by using `mounted` as an expression within which the `configuration` is already evaluated as a Hash.\n\n```ruby\nclass ExpressionEndpointAPI \u003C Grape::API\n  get(mounted { configuration[:route_name] || 'default_name' }) do\n    # some logic\n  end\nend\n```\n\n```ruby\nclass BasicAPI \u003C Grape::API\n  desc 'Statuses index' do\n    params: (configuration[:entity] || API::Entities::Status).documentation\n  end\n  params do\n    requires :all, using: (configuration[:entity] || API::Entities::Status).documentation\n  end\n  get '\u002Fstatuses' do\n    statuses = Status.all\n    type = current_user.admin? ? :full : :default\n    present statuses, with: (configuration[:entity] || API::Entities::Status), type: type\n  end\nend\n\nclass V1 \u003C Grape::API\n  version 'v1'\n  mount BasicAPI, with: { entity: mounted { configuration[:entity] || API::Entities::Status } }\nend\n\nclass V2 \u003C Grape::API\n  version 'v2'\n  mount BasicAPI, with: { entity: mounted { configuration[:entity] || API::Entities::V2::Status } }\nend\n```\n\n## Versioning\n\nYou have the option to provide various versions of your API by establishing a separate `Grape::API` class for each offered version and then integrating them into a primary `Grape::API` class. Ensure that newer versions are mounted before older ones. The default approach to versioning directs the request to the subsequent Rack middleware if a specific version is not found.\n\n```ruby\nrequire 'v1'\nrequire 'v2'\nrequire 'v3'\nclass App \u003C Grape::API\n  mount V3\n  mount V2\n  mount V1\nend\n```\n\nTo maintain the same endpoints from earlier API versions without rewriting them, you can indicate multiple versions within the previous API versions.\n\n```ruby\nclass V1 \u003C Grape::API\n  version 'v1', 'v2', 'v3'\n\n  get '\u002Ffoo' do\n    # your code for GET \u002Ffoo\n  end\n\n  get '\u002Fother' do\n    # your code for GET \u002Fother\n  end\nend\n\nclass V2 \u003C Grape::API\n  version 'v2', 'v3'\n\n  get '\u002Fvar' do\n    # your code for GET \u002Fvar\n  end\nend\n\nclass V3 \u003C Grape::API\n  version 'v3'\n\n  get '\u002Ffoo' do\n    # your new code for GET \u002Ffoo\n  end\nend\n```\n\nUsing the example provided, the subsequent endpoints will be accessible across various versions:\n\n```shell\nGET \u002Fv1\u002Ffoo\nGET \u002Fv1\u002Fother\nGET \u002Fv2\u002Ffoo # => Same behavior as v1\nGET \u002Fv2\u002Fother # => Same behavior as v1\nGET \u002Fv2\u002Fvar # => New endpoint not available in v1\nGET \u002Fv3\u002Ffoo # => Different behavior to v1 and v2\nGET \u002Fv3\u002Fother # => Same behavior as v1 and v2\nGET \u002Fv3\u002Fvar # => Same behavior as v2\n```\n\nThere are four strategies in which clients can reach your API's endpoints: `:path`, `:header`, `:accept_version_header` and `:param`. The default strategy is `:path`.\n\n### Strategies\n\n#### Path\n\n```ruby\nversion 'v1', using: :path\n```\n\nUsing this versioning strategy, clients should pass the desired version in the URL.\n\n    curl http:\u002F\u002Flocalhost:9292\u002Fv1\u002Fstatuses\u002Fpublic_timeline\n\n#### Header\n\n```ruby\nversion 'v1', using: :header, vendor: 'twitter'\n```\n\nCurrently, Grape only supports versioned media types in the following format:\n\n```\nvnd.vendor-and-or-resource-v1234+format\n```\n\nBasically all tokens between the final `-` and the `+` will be interpreted as the version.\n\nUsing this versioning strategy, clients should pass the desired version in the HTTP `Accept` head.\n\n    curl -H Accept:application\u002Fvnd.twitter-v1+json http:\u002F\u002Flocalhost:9292\u002Fstatuses\u002Fpublic_timeline\n\nBy default, the first matching version is used when no `Accept` header is supplied. This behavior is similar to routing in Rails. To circumvent this default behavior, one could use the `:strict` option. When this option is set to `true`, a `406 Not Acceptable` error is returned when no correct `Accept` header is supplied.\n\nWhen an invalid `Accept` header is supplied, a `406 Not Acceptable` error is returned if the `:cascade` option is set to `false`. Otherwise a `404 Not Found` error is returned by Rack if no other route matches.\n\nGrape will evaluate the relative quality preference included in Accept headers and default to a quality of 1.0 when omitted. In the following example a Grape API that supports XML and JSON in that order will return JSON:\n\n    curl -H \"Accept: text\u002Fxml;q=0.8, application\u002Fjson;q=0.9\" localhost:1234\u002Fresource\n\n#### Accept-Version Header\n\n```ruby\nversion 'v1', using: :accept_version_header\n```\n\nUsing this versioning strategy, clients should pass the desired version in the HTTP `Accept-Version` header.\n\n    curl -H \"Accept-Version:v1\" http:\u002F\u002Flocalhost:9292\u002Fstatuses\u002Fpublic_timeline\n\nBy default, the first matching version is used when no `Accept-Version` header is supplied. This behavior is similar to routing in Rails. To circumvent this default behavior, one could use the `:strict` option. When this option is set to `true`, a `406 Not Acceptable` error is returned when no correct `Accept` header is supplied and the `:cascade` option is set to `false`. Otherwise a `404 Not Found` error is returned by Rack if no other route matches.\n\n#### Param\n\n```ruby\nversion 'v1', using: :param\n```\n\nUsing this versioning strategy, clients should pass the desired version as a request parameter, either in the URL query string or in the request body.\n\n    curl http:\u002F\u002Flocalhost:9292\u002Fstatuses\u002Fpublic_timeline?apiver=v1\n\nThe default name for the query parameter is 'apiver' but can be specified using the `:parameter` option.\n\n```ruby\nversion 'v1', using: :param, parameter: 'v'\n```\n\n    curl http:\u002F\u002Flocalhost:9292\u002Fstatuses\u002Fpublic_timeline?v=v1\n\n\n## Linting\n\nYou can check whether your API is in conformance with the [Rack's specification](https:\u002F\u002Fgithub.com\u002Frack\u002Frack\u002Fblob\u002Fmain\u002FSPEC.rdoc) by calling `lint!` at the API level or through [configuration](#configuration).\n\n```ruby\nclass Api \u003C Grape::API\n  lint!\nend\n```\n```ruby\nGrape.configure do |config|\n  config.lint = true\nend\n```\n```ruby\nGrape.config.lint = true\n```\n\n### Bug in Rack::ETag under Rack 3.X\nIf you're using Rack 3.X and the `Rack::Etag` middleware (used by [Rails](https:\u002F\u002Fguides.rubyonrails.org\u002Frails_on_rack.html#inspecting-middleware-stack)), a [bug](https:\u002F\u002Fgithub.com\u002Frack\u002Frack\u002Fpull\u002F2324) related to linting has been fixed in [3.1.13](https:\u002F\u002Fgithub.com\u002Frack\u002Frack\u002Fblob\u002Fv3.1.13\u002FCHANGELOG.md#3113---2025-04-13) and [3.0.15](https:\u002F\u002Fgithub.com\u002Frack\u002Frack\u002Fblob\u002Fv3.1.13\u002FCHANGELOG.md#3015---2025-04-13) respectively.\n\n## Describing Methods\n\nYou can add a description to API methods and namespaces. The description would be used by [grape-swagger][grape-swagger] to generate swagger compliant documentation.\n\nNote: Description block is only for documentation and won't affects API behavior.\n\n```ruby\ndesc 'Returns your public timeline.' do\n  summary 'summary'\n  detail 'more details'\n  params  API::Entities::Status.documentation\n  success API::Entities::Entity\n  failure [[401, 'Unauthorized', 'Entities::Error']]\n  default { code: 500, message: 'InvalidRequest', model: Entities::Error }\n  named 'My named route'\n  headers XAuthToken: {\n            description: 'Validates your identity',\n            required: true\n          },\n          XOptionalHeader: {\n            description: 'Not really needed',\n            required: false\n          }\n  hidden false\n  deprecated false\n  is_array true\n  nickname 'nickname'\n  produces ['application\u002Fjson']\n  consumes ['application\u002Fjson']\n  tags ['tag1', 'tag2']\nend\nget :public_timeline do\n  Status.limit(20)\nend\n```\n\n* `detail`: A more enhanced description\n* `params`: Define parameters directly from an `Entity`\n* `success`: (former entity) The `Entity` to be used to present the success response for this route.\n* `failure`: (former http_codes) A definition of the used failure HTTP Codes and Entities.\n* `default`: The definition and `Entity` used to present the default response for this route.\n* `named`: A helper to give a route a name and find it with this name in the documentation Hash\n* `headers`: A definition of the used Headers\n* Other options can be found in [grape-swagger][grape-swagger]\n\n[grape-swagger]: https:\u002F\u002Fgithub.com\u002Fruby-grape\u002Fgrape-swagger\n\n## Configuration\n\nUse `Grape.configure` to set up global settings at load time.\nCurrently the configurable settings are:\n\n* `param_builder`: Sets the [Parameter Builder](#parameters), defaults to `Grape::Extensions::ActiveSupport::HashWithIndifferentAccess::ParamBuilder`.\n\nTo change a setting value make sure that at some point during load time the following code runs\n\n```ruby\nGrape.configure do |config|\n  config.setting = value\nend\n```\n\nFor example, for the `param_builder`, the following code could run in an initializer:\n\n```ruby\nGrape.configure do |config|\n  config.param_builder = :hashie_mash\nend\n```\n\nAvailable parameter builders are `:hash`, `:hash_with_indifferent_access`, and `:hashie_mash`.\nSee [params_builder](lib\u002Fgrape\u002Fparams_builder).\n\nYou can also configure a single API:\n\n```ruby\nAPI.configure do |config|\n  config[key] = value\nend\n```\n\nThis will be available inside the API with `configuration`, as if it were [mount configuration](#mount-configuration).\n\n## Parameters\n\nRequest parameters are available through the `params` hash object. This includes `GET`, `POST` and `PUT` parameters, along with any named parameters you specify in your route strings.\n\n```ruby\nget :public_timeline do\n  Status.order(params[:sort_by])\nend\n```\n\nParameters are automatically populated from the request body on `POST` and `PUT` for form input, JSON and XML content-types.\n\nThe request:\n\n```\ncurl -d '{\"text\": \"140 characters\"}' 'http:\u002F\u002Flocalhost:9292\u002Fstatuses' -H Content-Type:application\u002Fjson -v\n```\n\nThe Grape endpoint:\n\n```ruby\npost '\u002Fstatuses' do\n  Status.create!(text: params[:text])\nend\n```\n\nMultipart POSTs and PUTs are supported as well.\n\nThe request:\n\n```\ncurl --form image_file='@image.jpg;type=image\u002Fjpg' http:\u002F\u002Flocalhost:9292\u002Fupload\n```\n\nThe Grape endpoint:\n\n```ruby\npost 'upload' do\n  # file in params[:image_file]\nend\n```\n\nIn the case of conflict between either of:\n\n* route string parameters\n* `GET`, `POST` and `PUT` parameters\n* the contents of the request body on `POST` and `PUT`\n\nRoute string parameters will have precedence.\n\n### Params Class\n\nBy default parameters are available as `ActiveSupport::HashWithIndifferentAccess`. This can be changed to, for example, Ruby `Hash` or `Hashie::Mash` for the entire API.\n\n```ruby\nclass API \u003C Grape::API\n  build_with :hashie_mash\n\n  params do\n    optional :color, type: String\n  end\n  get do\n    params.color # instead of params[:color]\n  end\n```\n\nThe class can also be overridden on individual parameter blocks using `build_with` as follows.\n\n```ruby\nparams do\n  build_with :hash\n  optional :color, type: String\nend\n```\n\nIn the example above, `params[\"color\"]` will return `nil` since `params` is a plain `Hash`.\n\nAvailable parameter builders are `:hash`, `:hash_with_indifferent_access`, and `:hashie_mash`.\nSee [params_builder](lib\u002Fgrape\u002Fparams_builder).\n\n### Declared\n\nGrape allows you to access only the parameters that have been declared by your `params` block. It will:\n\n  * Filter out the params that have been passed, but are not allowed.\n  * Include any optional params that are declared but not passed.\n  * Perform any parameter renaming on the resulting hash.\n\nConsider the following API endpoint:\n\n````ruby\nformat :json\n\npost 'users\u002Fsignup' do\n  { 'declared_params' => declared(params) }\nend\n````\n\nIf you do not specify any parameters, `declared` will return an empty hash.\n\n**Request**\n\n````bash\ncurl -X POST -H \"Content-Type: application\u002Fjson\" localhost:9292\u002Fusers\u002Fsignup -d '{\"user\": {\"first_name\":\"first name\", \"last_name\": \"last name\"}}'\n````\n\n**Response**\n\n````json\n{\n  \"declared_params\": {}\n}\n\n````\n\nOnce we add parameters requirements, grape will start returning only the declared parameters.\n\n````ruby\nformat :json\n\nparams do\n  optional :user, type: Hash do\n    optional :first_name, type: String\n    optional :last_name, type: String\n  end\nend\n\npost 'users\u002Fsignup' do\n  { 'declared_params' => declared(params) }\nend\n````\n\n**Request**\n\n````bash\ncurl -X POST -H \"Content-Type: application\u002Fjson\" localhost:9292\u002Fusers\u002Fsignup -d '{\"user\": {\"first_name\":\"first name\", \"last_name\": \"last name\", \"random\": \"never shown\"}}'\n````\n\n**Response**\n\n````json\n{\n  \"declared_params\": {\n    \"user\": {\n      \"first_name\": \"first name\",\n      \"last_name\": \"last name\"\n    }\n  }\n}\n````\n\nMissing params that are declared as type `Hash` or `Array` will be included.\n\n````ruby\nformat :json\n\nparams do\n  optional :user, type: Hash do\n    optional :first_name, type: String\n    optional :last_name, type: String\n  end\n  optional :widgets, type: Array\nend\n\npost 'users\u002Fsignup' do\n  { 'declared_params' => declared(params) }\nend\n````\n\n**Request**\n\n````bash\ncurl -X POST -H \"Content-Type: application\u002Fjson\" localhost:9292\u002Fusers\u002Fsignup -d '{}'\n````\n\n**Response**\n\n````json\n{\n  \"declared_params\": {\n    \"user\": {\n      \"first_name\": null,\n      \"last_name\": null\n    },\n    \"widgets\": []\n  }\n}\n````\n\nThe returned hash is an `ActiveSupport::HashWithIndifferentAccess`.\n\nThe `#declared` method is not available to `before` filters, as those are evaluated prior to parameter coercion.\n\n### Include Parent Namespaces\n\nBy default `declared(params)` includes parameters that were defined in all parent namespaces. If you want to return only parameters from your current namespace, you can set `include_parent_namespaces` option to `false`.\n\n````ruby\nformat :json\n\nnamespace :parent do\n  params do\n    requires :parent_name, type: String\n  end\n\n  namespace ':parent_name' do\n    params do\n      requires :child_name, type: String\n    end\n    get ':child_name' do\n      {\n        'without_parent_namespaces' => declared(params, include_parent_namespaces: false),\n        'with_parent_namespaces' => declared(params, include_parent_namespaces: true),\n      }\n    end\n  end\nend\n````\n\n**Request**\n\n````bash\ncurl -X GET -H \"Content-Type: application\u002Fjson\" localhost:9292\u002Fparent\u002Ffoo\u002Fbar\n````\n\n**Response**\n\n````json\n{\n  \"without_parent_namespaces\": {\n    \"child_name\": \"bar\"\n  },\n  \"with_parent_namespaces\": {\n    \"parent_name\": \"foo\",\n    \"child_name\": \"bar\"\n  },\n}\n````\n\n### Include Missing\n\nBy default `declared(params)` includes parameters that have `nil` values. If you want to return only the parameters that are not `nil`, you can use the `include_missing` option. By default, `include_missing` is set to `true`. Consider the following API:\n\n````ruby\nformat :json\n\nparams do\n  requires :user, type: Hash do\n    requires :first_name, type: String\n    optional :last_name, type: String\n  end\nend\n\npost 'users\u002Fsignup' do\n  { 'declared_params' => declared(params, include_missing: false) }\nend\n````\n\n**Request**\n\n````bash\ncurl -X POST -H \"Content-Type: application\u002Fjson\" localhost:9292\u002Fusers\u002Fsignup -d '{\"user\": {\"first_name\":\"first name\", \"random\": \"never shown\"}}'\n````\n\n**Response with include_missing:false**\n\n````json\n{\n  \"declared_params\": {\n    \"user\": {\n      \"first_name\": \"first name\"\n    }\n  }\n}\n````\n\n**Response with include_missing:true**\n\n````json\n{\n  \"declared_params\": {\n    \"user\": {\n      \"first_name\": \"first name\",\n      \"last_name\": null\n    }\n  }\n}\n````\n\nIt also works on nested hashes:\n\n````ruby\nformat :json\n\nparams do\n  requires :user, type: Hash do\n    requires :first_name, type: String\n    optional :last_name, type: String\n    requires :address, type: Hash do\n      requires :city, type: String\n      optional :region, type: String\n    end\n  end\nend\n\npost 'users\u002Fsignup' do\n  { 'declared_params' => declared(params, include_missing: false) }\nend\n````\n\n**Request**\n\n````bash\ncurl -X POST -H \"Content-Type: application\u002Fjson\" localhost:9292\u002Fusers\u002Fsignup -d '{\"user\": {\"first_name\":\"first name\", \"random\": \"never shown\", \"address\": { \"city\": \"SF\"}}}'\n````\n\n**Response with include_missing:false**\n\n````json\n{\n  \"declared_params\": {\n    \"user\": {\n      \"first_name\": \"first name\",\n      \"address\": {\n        \"city\": \"SF\"\n      }\n    }\n  }\n}\n````\n\n**Response with include_missing:true**\n\n````json\n{\n  \"declared_params\": {\n    \"user\": {\n      \"first_name\": \"first name\",\n      \"last_name\": null,\n      \"address\": {\n        \"city\": \"Zurich\",\n        \"region\": null\n      }\n    }\n  }\n}\n````\n\nNote that an attribute with a `nil` value is not considered *missing* and will also be returned when `include_missing` is set to `false`:\n\n**Request**\n\n````bash\ncurl -X POST -H \"Content-Type: application\u002Fjson\" localhost:9292\u002Fusers\u002Fsignup -d '{\"user\": {\"first_name\":\"first name\", \"last_name\": null, \"address\": { \"city\": \"SF\"}}}'\n````\n\n**Response with include_missing:false**\n\n````json\n{\n  \"declared_params\": {\n    \"user\": {\n      \"first_name\": \"first name\",\n      \"last_name\": null,\n      \"address\": { \"city\": \"SF\"}\n    }\n  }\n}\n````\n\n### Evaluate Given\n\nBy default `declared(params)` will not evaluate `given` and return all parameters. Use `evaluate_given` to evaluate all `given` blocks and return only parameters that satisfy `given` conditions. Consider the following API:\n\n````ruby\nformat :json\n\nparams do\n  optional :child_id, type: Integer\n  given :child_id do\n    requires :father_id, type: Integer\n  end\nend\n\npost 'child' do\n  { 'declared_params' => declared(params, evaluate_given: true) }\nend\n````\n\n**Request**\n\n````bash\ncurl -X POST -H \"Content-Type: application\u002Fjson\" localhost:9292\u002Fchild -d '{\"father_id\": 1}'\n````\n\n**Response with evaluate_given:false**\n\n````json\n{\n  \"declared_params\": {\n    \"child_id\": null,\n    \"father_id\": 1\n  }\n}\n````\n\n**Response with evaluate_given:true**\n\n````json\n{\n  \"declared_params\": {\n    \"child_id\": null\n  }\n}\n````\n\nIt also works on nested hashes:\n\n````ruby\nformat :json\n\nparams do\n  requires :child, type: Hash do\n    optional :child_id, type: Integer\n    given :child_id do\n      requires :father_id, type: Integer\n    end\n  end\nend\n\npost 'child' do\n  { 'declared_params' => declared(params, evaluate_given: true) }\nend\n````\n\n**Request**\n\n````bash\ncurl -X POST -H \"Content-Type: application\u002Fjson\" localhost:9292\u002Fchild -d '{\"child\": {\"father_id\": 1}}'\n````\n\n**Response with evaluate_given:false**\n\n````json\n{\n  \"declared_params\": {\n    \"child\": {\n      \"child_id\": null,\n      \"father_id\": 1\n    }\n  }\n}\n````\n\n**Response with evaluate_given:true**\n\n````json\n{\n  \"declared_params\": {\n    \"child\": {\n      \"child_id\": null\n    }\n  }\n}\n````\n\n### Parameter Precedence\n\nUsing `route_param` takes higher precedence over a regular parameter defined with same name:\n\n```ruby\nparams do\n  requires :foo, type: String\nend\nroute_param :foo do\n  get do\n    { value: params[:foo] }\n  end\nend\n```\n\n**Request**\n\n```bash\ncurl -X POST -H \"Content-Type: application\u002Fjson\" localhost:9292\u002Fbar -d '{\"foo\": \"baz\"}'\n```\n\n**Response**\n\n```json\n{\n  \"value\": \"bar\"\n}\n```\n\n## Parameter Validation and Coercion\n\nYou can define validations and coercion options for your parameters using a `params` block.\n\n```ruby\nparams do\n  requires :id, type: Integer\n  optional :text, type: String, regexp: \u002F\\A[a-z]+\\z\u002F\n  group :media, type: Hash do\n    requires :url\n  end\n  optional :audio, type: Hash do\n    requires :format, type: Symbol, values: [:mp3, :wav, :aac, :ogg], default: :mp3\n  end\n  mutually_exclusive :media, :audio\nend\nput ':id' do\n  # params[:id] is an Integer\nend\n```\n\nWhen a type is specified an implicit validation is done after the coercion to ensure the output type is the one declared.\n\nOptional parameters can have a default value.\n\n```ruby\nparams do\n  optional :color, type: String, default: 'blue'\n  optional :random_number, type: Integer, default: -> { Random.rand(1..100) }\n  optional :non_random_number, type: Integer, default:  Random.rand(1..100)\nend\n```\n\nDefault values are eagerly evaluated. Above `:non_random_number` will evaluate to the same number for each call to the endpoint of this `params` block. To have the default evaluate lazily with each request use a lambda, like `:random_number` above.\n\nNote that default values will be passed through to any validation options specified.\nThe following example will always fail if `:color` is not explicitly provided.\n\n```ruby\nparams do\n  optional :color, type: String, default: 'blue', values: ['red', 'green']\nend\n```\n\nThe correct implementation is to ensure the default value passes all validations.\n\n```ruby\nparams do\n  optional :color, type: String, default: 'blue', values: ['blue', 'red', 'green']\nend\n```\n\nYou can use the value of one parameter as the default value of some other parameter. In this case, if the `primary_color` parameter is not provided, it will have the same value as the `color` one. If both of them not provided, both of them will have `blue` value.\n\n```ruby\nparams do\n  optional :color, type: String, default: 'blue'\n  optional :primary_color, type: String, default: -> (params) { params[:color] }\nend\n```\n\n### Supported Parameter Types\n\nThe following are all valid types, supported out of the box by Grape:\n\n* Integer\n* Float\n* BigDecimal\n* Numeric\n* Date\n* DateTime\n* Time\n* Boolean\n* String\n* Symbol\n* Rack::Multipart::UploadedFile (alias `File`)\n* JSON\n\n### Integer\u002FFixnum and Coercions\n\nPlease be aware that the behavior differs between Ruby 2.4 and earlier versions.\nIn Ruby 2.4, values consisting of numbers are converted to Integer, but in earlier versions it will be treated as Fixnum.\n\n```ruby\nparams do\n  requires :integers, type: Hash do\n    requires :int, coerce: Integer\n  end\nend\nget '\u002Fint' do\n  params[:integers][:int].class\nend\n\n...\n\nget '\u002Fint' integers: { int: '45' }\n  #=> Integer in ruby 2.4\n  #=> Fixnum in earlier ruby versions\n```\n\n### Custom Types and Coercions\n\nAside from the default set of supported types listed above, any class can be used as a type as long as an explicit coercion method is supplied. If the type implements a class-level `parse` method, Grape will use it automatically. This method must take one string argument and return an instance of the correct type, or return an instance of `Grape::Types::InvalidValue` which optionally accepts a message to be returned in the response.\n\n```ruby\nclass Color\n  attr_reader :value\n  def initialize(color)\n    @value = color\n  end\n\n  def self.parse(value)\n    return new(value) if %w[blue red green].include?(value)\n\n    Grape::Types::InvalidValue.new('Unsupported color')\n  end\nend\n\nparams do\n  requires :color, type: Color, default: Color.new('blue')\n  requires :more_colors, type: Array[Color] # Collections work\n  optional :unique_colors, type: Set[Color] # Duplicates discarded\nend\n\nget '\u002Fstuff' do\n  # params[:color] is already a Color.\n  params[:color].value\nend\n```\n\nAlternatively, a custom coercion method may be supplied for any type of parameter using `coerce_with`. Any class or object may be given that implements a `parse` or `call` method, in that order of precedence. The method must accept a single string parameter, and the return value must match the given `type`.\n\n```ruby\nparams do\n  requires :passwd, type: String, coerce_with: Base64.method(:decode64)\n  requires :loud_color, type: Color, coerce_with: ->(c) { Color.parse(c.downcase) }\n\n  requires :obj, type: Hash, coerce_with: JSON do\n    requires :words, type: Array[String], coerce_with: ->(val) { val.split(\u002F\\s+\u002F) }\n    optional :time, type: Time, coerce_with: Chronic\n  end\nend\n```\nNote that, a `nil` value will call the custom coercion method, while a missing parameter will not.\n\nExample of use of `coerce_with` with a lambda (a class with a `parse` method could also have been used)\nIt will parse a string and return an Array of Integers, matching the `Array[Integer]` `type`.\n\n```ruby\nparams do\n  requires :values, type: Array[Integer], coerce_with: ->(val) { val.split(\u002F\\s+\u002F).map(&:to_i) }\nend\n```\n\nGrape will assert that coerced values match the given `type`, and will reject the request if they do not. To override this behaviour, custom types may implement a `parsed?` method that should accept a single argument and return `true` if the value passes type validation.\n\n```ruby\nclass SecureUri\n  def self.parse(value)\n    URI.parse value\n  end\n\n  def self.parsed?(value)\n    value.is_a? URI::HTTPS\n  end\nend\n\nparams do\n  requires :secure_uri, type: SecureUri\nend\n```\n\n### Multipart File Parameters\n\nGrape makes use of `Rack::Request`'s built-in support for multipart file parameters. Such parameters can be declared with `type: File`:\n\n```ruby\nparams do\n  requires :avatar, type: File\nend\npost '\u002F' do\n  params[:avatar][:filename] # => 'avatar.png'\n  params[:avatar][:type] # => 'image\u002Fpng'\n  params[:avatar][:tempfile] # => #\u003CFile>\nend\n```\n\n### First-Class `JSON` Types\n\nGrape supports complex parameters given as JSON-formatted strings using the special `type: JSON` declaration. JSON objects and arrays of objects are accepted equally, with nested validation rules applied to all objects in either case:\n\n```ruby\nparams do\n  requires :json, type: JSON do\n    requires :int, type: Integer, values: [1, 2, 3]\n  end\nend\nget '\u002F' do\n  params[:json].inspect\nend\n\nclient.get('\u002F', json: '{\"int\":1}') # => \"{:int=>1}\"\nclient.get('\u002F', json: '[{\"int\":\"1\"}]') # => \"[{:int=>1}]\"\n\nclient.get('\u002F', json: '{\"int\":4}') # => HTTP 400\nclient.get('\u002F', json: '[{\"int\":4}]') # => HTTP 400\n```\n\nAdditionally `type: Array[JSON]` may be used, which explicitly marks the parameter as an array of objects. If a single object is supplied it will be wrapped.\n\n```ruby\nparams do\n  requires :json, type: Array[JSON] do\n    requires :int, type: Integer\n  end\nend\nget '\u002F' do\n  params[:json].each { |obj| ... } # always works\nend\n```\nFor stricter control over the type of JSON structure which may be supplied, use `type: Array, coerce_with: JSON` or `type: Hash, coerce_with: JSON`.\n\n### Multiple Allowed Types\n\nVariant-type parameters can be declared using the `types` option rather than `type`:\n\n```ruby\nparams do\n  requires :status_code, types: [Integer, String, Array[Integer, String]]\nend\nget '\u002F' do\n  params[:status_code].inspect\nend\n\nclient.get('\u002F', status_code: 'OK_GOOD') # => \"OK_GOOD\"\nclient.get('\u002F', status_code: 300) # => 300\nclient.get('\u002F', status_code: %w(404 NOT FOUND)) # => [404, \"NOT\", \"FOUND\"]\n```\n\nAs a special case, variant-member-type collections may also be declared, by passing a `Set` or `Array` with more than one member to `type`:\n\n```ruby\nparams do\n  requires :status_codes, type: Array[Integer,String]\nend\nget '\u002F' do\n  params[:status_codes].inspect\nend\n\nclient.get('\u002F', status_codes: %w(1 two)) # => [1, \"two\"]\n```\n\n### Validation of Nested Parameters\n\nParameters can be nested using `group` or by calling `requires` or `optional` with a block.\nIn the [above example](#parameter-validation-and-coercion), this means `params[:media][:url]` is required along with `params[:id]`, and `params[:audio][:format]` is required only if `params[:audio]` is present.\nWith a block, `group`, `requires` and `optional` accept an additional option `type` which can be either `Array` or `Hash`, and defaults to `Array`. Depending on the value, the nested parameters will be treated either as values of a hash or as values of hashes in an array.\n\n```ruby\nparams do\n  optional :preferences, type: Array do\n    requires :key\n    requires :value\n  end\n\n  requires :name, type: Hash do\n    requires :first_name\n    requires :last_name\n  end\nend\n```\n\n### Dependent Parameters\n\nSuppose some of your parameters are only relevant if another parameter is given; Grape allows you to express this relationship through the `given` method in your parameters block, like so:\n\n```ruby\nparams do\n  optional :shelf_id, type: Integer\n  given :shelf_id do\n    requires :bin_id, type: Integer\n  end\nend\n```\n\nIn the example above Grape will use `blank?` to check whether the `shelf_id` param is present.\n\n`given` also takes a `Proc` with custom code. Below, the param `description` is required only if the value of `category` is equal `foo`:\n\n```ruby\nparams do\n  optional :category\n  given category: ->(val) { val == 'foo' } do\n    requires :description\n  end\nend\n```\n\nYou can rename parameters:\n\n```ruby\nparams do\n  optional :category, as: :type\n  given type: ->(val) { val == 'foo' } do\n    requires :description\n  end\nend\n```\n\nNote: param in `given` should be the renamed one. In the example, it should be `type`, not `category`.\n\n### Group Options\n\nParameters options can be grouped. It can be useful if you want to extract common validation or types for several parameters.\nWithin these groups, individual parameters can extend or selectively override the common settings, allowing you to maintain the defaults at the group level while still applying parameter-specific rules where necessary.\n\nThe example below presents a typical case when parameters share common options.\n\n```ruby\nparams do\n  requires :first_name, type: String, regexp: \u002Fw+\u002F, desc: 'First name', documentation: { in: 'body' }\n  optional :middle_name, type: String, regexp: \u002Fw+\u002F, desc: 'Middle name', documentation: { in: 'body', x: { nullable: true } }\n  requires :last_name, type: String, regexp: \u002Fw+\u002F, desc: 'Last name', documentation: { in: 'body' }\nend\n```\n\nGrape allows you to present the same logic through the `with` method in your parameters block, like so:\n\n```ruby\nparams do\n  with(type: String, regexp: \u002Fw+\u002F, documentation: { in: 'body' }) do\n    requires :first_name, desc: 'First name'\n    optional :middle_name, desc: 'Middle name', documentation: { x: { nullable: true } }\n    requires :last_name, desc: 'Last name'\n  end\nend\n```\n\nYou can organize settings into layers using nested `with' blocks. Each layer can use, add to, or change the settings of the layer above it. This helps to keep complex parameters organized and consistent, while still allowing for specific customizations to be made.\n\n```ruby\nparams do\n  with(documentation: { in: 'body' }) do  # Applies documentation to all nested parameters\n    with(type: String, regexp: \u002F\\w+\u002F) do  # Applies type and validation to names\n      requires :first_name, desc: 'First name'\n      requires :last_name, desc: 'Last name'\n    end\n    optional :age, type: Integer, desc: 'Age', documentation: { x: { nullable: true } }  # Specific settings for 'age'\n  end\nend\n```\n\n### Renaming\n\nYou can rename parameters using `as`, which can be useful when refactoring existing APIs:\n\n```ruby\nresource :users do\n  params do\n    requires :email_address, as: :email\n    requires :password\n  end\n  post do\n    User.create!(declared(params)) # User takes email and password\n  end\nend\n```\n\nThe value passed to `as` will be the key when calling `declared(params)`.\n\n### Built-in Validators\n\n#### `allow_blank`\n\nParameters can be defined as `allow_blank`, ensuring that they contain a value. By default, `requires` only validates that a parameter was sent in the request, regardless its value. With `allow_blank: false`, empty values or whitespace only values are invalid.\n\n`allow_blank` can be combined with both `requires` and `optional`. If the parameter is required, it has to contain a value. If it's optional, it's possible to not send it in the request, but if it's being sent, it has to have some value, and not an empty string\u002Fonly whitespaces.\n\n\n```ruby\nparams do\n  requires :username, allow_blank: false\n  optional :first_name, allow_blank: false\nend\n```\n\n#### `values`\n\nParameters can be restricted to a specific set of values with the `:values` option.\n\n\n```ruby\nparams do\n  requires :status, type: Symbol, values: [:not_started, :processing, :done]\n  optional :numbers, type: Array[Integer], default: 1, values: [1, 2, 3, 5, 8]\nend\n```\n\nSupplying a range to the `:values` option ensures that the parameter is (or parameters are) included in that range (using `Range#include?`).\n\n```ruby\nparams do\n  requires :latitude, type: Float, values: -90.0..+90.0\n  requires :longitude, type: Float, values: -180.0..+180.0\n  optional :letters, type: Array[String], values: 'a'..'z'\nend\n```\n\nNote endless ranges are also supported with ActiveSupport >= 6.0, but they require that the type be provided.\n\n```ruby\nparams do\n  requires :minimum, type: Integer, values: 10..\n  optional :maximum, type: Integer, values: ..10\nend\n```\n\nNote that *both* range endpoints have to be a `#kind_of?` your `:type` option (if you don't supply the `:type` option, it will be guessed to be equal to the class of the range's first endpoint). So the following is invalid:\n\n```ruby\nparams do\n  requires :invalid1, type: Float, values: 0..10 # 0.kind_of?(Float) => false\n  optional :invalid2, values: 0..10.0 # 10.0.kind_of?(0.class) => false\nend\n```\n\nThe `:values` option can also be supplied with a `Proc`, evaluated lazily with each request.\nIf the Proc has arity zero (i.e. it takes no arguments) it is expected to return either a list or a range which will then be used to validate the parameter.\n\nFor example, given a status model you may want to restrict by hashtags that you have previously defined in the `HashTag` model.\n\n```ruby\nparams do\n  requires :hashtag, type: String, values: -> { Hashtag.all.map(&:tag) }\nend\n```\n\nAlternatively, a Proc with arity one (i.e. taking one argument) can be used to explicitly validate each parameter value.  In that case, the Proc is expected to return a truthy value if the parameter value is valid. The parameter will be considered invalid if the Proc returns a falsy value or if it raises a StandardError.\n\n```ruby\nparams do\n  requires :number, type: Integer, values: ->(v) { v.even? && v \u003C 25 }\nend\n```\n\nWhile Procs are convenient for single cases, consider using [Custom Validators](#custom-validators) in cases where a validation is used more than once.\n\nNote that [allow_blank](#allow_blank) validator applies while using `:values`. In the following example the absence of `:allow_blank` does not prevent `:state` from receiving blank values because `:allow_blank` defaults to `true`.\n\n```ruby\nparams do\n  requires :state, type: Symbol, values: [:active, :inactive]\nend\n```\n\n#### `except_values`\n\nParameters can be restricted from having a specific set of values with the `:except_values` option.\n\nThe `except_values` validator behaves similarly to the `values` validator in that it accepts either an Array, a Range, or a Proc.  Unlike the `values` validator, however, `except_values` only accepts Procs with arity zero.\n\n```ruby\nparams do\n  requires :browser, except_values: [ 'ie6', 'ie7', 'ie8' ]\n  requires :port, except_values: { value: 0..1024, message: 'is not allowed' }\n  requires :hashtag, except_values: -> { Hashtag.FORBIDDEN_LIST }\nend\n```\n\n#### `same_as`\n\nA `same_as` option can be given to ensure that values of parameters match.\n\n```ruby\nparams do\n  requires :password\n  requires :password_confirmation, same_as: :password\nend\n```\n\n#### `length`\n\nParameters with types that support `#length` method can be restricted to have a specific length with the `:length` option.\n\nThe validator accepts `:min` or `:max` or both options or only `:is` to validate that the value of the parameter is within the given limits.\n\n```ruby\nparams do\n  requires :code, type: String, length: { is: 2 }\n  requires :str, type: String, length: { min: 3 }\n  requires :list, type: [Integer], length: { min: 3, max: 5 }\n  requires :hash, type: Hash, length: { max: 5 }\nend\n```\n\n#### `regexp`\n\nParameters can be restricted to match a specific regular expression with the `:regexp` option. If the value does not match the regular expression an error will be returned. Note that this is true for both `requires` and `optional` parameters.\n\n```ruby\nparams do\n  requires :email, regexp: \u002F.+@.+\u002F\nend\n```\n\nThe validator will pass if the parameter was sent without value. To ensure that the parameter contains a value, use `allow_blank: false`.\n\n```ruby\nparams do\n  requires :email, allow_blank: false, regexp: \u002F.+@.+\u002F\nend\n```\n\n#### `mutually_exclusive`\n\nParameters can be defined as `mutually_exclusive`, ensuring that they aren't present at the same time in a request.\n\n```ruby\nparams do\n  optional :beer\n  optional :wine\n  mutually_exclusive :beer, :wine\nend\n```\n\nMultiple sets can be defined:\n\n```ruby\nparams do\n  optional :beer\n  optional :wine\n  mutually_exclusive :beer, :wine\n  optional :scotch\n  optional :aquavit\n  mutually_exclusive :scotch, :aquavit\nend\n```\n\n**Warning**: Never define mutually exclusive sets with any required params. Two mutually exclusive required params will mean params are never valid, thus making the endpoint useless. One required param mutually exclusive with an optional param will mean the latter is never valid.\n\n#### `exactly_one_of`\n\nParameters can be defined as 'exactly_one_of', ensuring that exactly one parameter gets selected.\n\n```ruby\nparams do\n  optional :beer\n  optional :wine\n  exactly_one_of :beer, :wine\nend\n```\n\nNote that using `:default` with `mutually_exclusive` will cause multiple parameters to always have a default value and raise a `Grape::Exceptions::Validation` mutually exclusive exception.\n\n#### `at_least_one_of`\n\nParameters can be defined as 'at_least_one_of', ensuring that at least one parameter gets selected.\n\n```ruby\nparams do\n  optional :beer\n  optional :wine\n  optional :juice\n  at_least_one_of :beer, :wine, :juice\nend\n```\n\n#### `all_or_none_of`\n\nParameters can be defined as 'all_or_none_of', ensuring that all or none of parameters gets selected.\n\n```ruby\nparams do\n  optional :beer\n  optional :wine\n  optional :juice\n  all_or_none_of :beer, :wine, :juice\nend\n```\n\n#### Nested `mutually_exclusive`, `exactly_one_of`, `at_least_one_of`, `all_or_none_of`\n\nAll of these methods can be used at any nested level.\n\n```ruby\nparams do\n  requires :food, type: Hash do\n    optional :meat\n    optional :fish\n    optional :rice\n    at_least_one_of :meat, :fish, :rice\n  end\n  group :drink, type: Hash do\n    optional :beer\n    optional :wine\n    optional :juice\n    exactly_one_of :beer, :wine, :juice\n  end\n  optional :dessert, type: Hash do\n    optional :cake\n    optional :icecream\n    mutually_exclusive :cake, :icecream\n  end\n  optional :recipe, type: Hash do\n    optional :oil\n    optional :meat\n    all_or_none_of :oil, :meat\n  end\nend\n```\n\n### Namespace Validation and Coercion\n\nNamespaces allow parameter definitions and apply to every method within the namespace.\n\n```ruby\nnamespace :statuses do\n  params do\n    requires :user_id, type: Integer, desc: 'A user ID.'\n  end\n  namespace ':user_id' do\n    desc \"Retrieve a user's status.\"\n    params do\n      requires :status_id, type: Integer, desc: 'A status ID.'\n    end\n    get ':status_id' do\n      User.find(params[:user_id]).statuses.find(params[:status_id])\n    end\n  end\nend\n```\n\nThe `namespace` method has a number of aliases, including: `group`, `resource`, `resources`, and `segment`. Use whichever reads the best for your API.\n\nYou can conveniently define a route parameter as a namespace using `route_param`.\n\n```ruby\nnamespace :statuses do\n  route_param :id do\n    desc 'Returns all replies for a status.'\n    get 'replies' do\n      Status.find(params[:id]).replies\n    end\n    desc 'Returns a status.'\n    get do\n      Status.find(params[:id])\n    end\n  end\nend\n```\n\nYou can also define a route parameter type by passing to `route_param`'s options.\n\n```ruby\nnamespace :arithmetic do\n  route_param :n, type: Integer do\n    desc 'Returns in power'\n    get 'power' do\n      params[:n] ** params[:n]\n    end\n  end\nend\n```\n\n### Custom Validators\n\n```ruby\nclass AlphaNumeric \u003C Grape::Validations::Validators::Base\n  def validate_param!(attr_name, params)\n    return if params[attr_name].match?(\u002F\\A[[:alnum:]]+\\z\u002F)\n\n    validation_error!(attr_name, 'must consist of alpha-numeric characters')\n  end\nend\n```\n\n```ruby\nparams do\n  requires :text, alpha_numeric: true\nend\n```\n\nYou can also create custom classes that take parameters.\n\n```ruby\nclass Length \u003C Grape::Validations::Validators::Base\n  def validate_param!(attr_name, params)\n    return if params[attr_name].length \u003C= @options\n\n    validation_error!(attr_name, \"must be at the most #{@options} characters long\")\n  end\nend\n```\n\n```ruby\nparams do\n  requires :text, length: 140\nend\n```\n\nYou can also create custom validation that use request to validate the attribute. For example if you want to have parameters that are available to only admins, you can do the following.\n\n```ruby\nclass Admin \u003C Grape::Validations::Validators::Base\n  def validate(request)\n    # return if the param we are checking was not in request\n    # @attrs is a list containing the attribute we are currently validating\n    # in our sample case this method once will get called with\n    # @attrs being [:admin_field] and once with @attrs being [:admin_false_field]\n    return unless request.params.key?(@attrs.first)\n    # check if admin flag is set to true\n    return unless @options\n    # check if user is admin or not\n    # as an example get a token from request and check if it's admin or not\n    validation_error!(@attrs, 'Can not set admin-only field.') unless request.headers['X-Access-Token'] == 'admin'\n  end\nend\n```\n\nAnd use it in your endpoint definition as:\n\n```ruby\nparams do\n  optional :admin_field, type: String, admin: true\n  optional :non_admin_field, type: String\n  optional :admin_false_field, type: String, admin: false\nend\n```\n\nEach validator is instantiated once at route definition time and frozen. Any setup (option parsing, message building) should happen in `initialize`, not in `validate_param!` or `validate`.\n\n#### Available helpers\n\nThe following protected\u002Fprivate helpers are available in any `Grape::Validations::Validators::Base` subclass:\n\n| Helper | Description |\n|---|---|\n| `default_message_key(key)` | Class-level macro. Declares the default I18n key for `validation_error!`. A per-option `:message` override still takes precedence. |\n| `validation_error!(attr_name_or_params, message = @exception_message)` | Raises `Grape::Exceptions::Validation`. Accepts a single attribute name or a pre-computed array of full param names. |\n| `@options` | The validator option value, deep-frozen at initialization. |\n| `@attrs` | Frozen array of attribute names this validator applies to. |\n| `@scope` | The `ParamsScope` — use `@scope.full_name(attr_name)` for the fully-qualified param name. |\n| `option_value` | Returns `@options[:value]` if present, otherwise `@options`. |\n| `options_key?(key)` | Returns true if `@options` is a hash with a non-nil `key`. |\n| `hash_like?(obj)` | Returns true if `obj` responds to `key?`. |\n| `scrub(value)` | Returns `value` with invalid byte sequences scrubbed. |\n| `translate(key, **opts)` | I18n lookup with `:en` fallback and `grape.errors.messages` scope. Called at request time to respect per-request locale. |\n\nUse `default_message_key` for a fixed I18n key. The message is resolved once at route definition time via `message`, so a per-option `:message` override still wins:\n\n```ruby\nclass SpecialValidator \u003C Grape::Validations::Validators::Base\n  default_message_key :special\n\n  def validate_param!(attr_name, params)\n    return if valid?(params[attr_name])\n\n    validation_error!(attr_name)\n  end\nend\n```\n\nFor interpolated messages that must respect per-request locale, call `translate` directly inside `validate_param!`:\n\n```ruby\nclass SpecialValidator \u003C Grape::Validations::Validators::Base\n  def validate_param!(attr_name, params)\n    return if valid?(params[attr_name])\n\n    validation_error!(attr_name, translate(:special, min: 2, max: 10))\n  end\nend\n```\n\n### Validation Errors\n\nValidation and coercion errors are collected and an exception of type `Grape::Exceptions::ValidationErrors` is raised. If the exception goes uncaught it will respond with a status of 400 and an error message. The validation errors are grouped by parameter name and can be accessed via `Grape::Exceptions::ValidationErrors#errors`.\n\n\nThe default response from a `Grape::Exceptions::ValidationErrors` is a humanly readable string, such as \"beer, wine are mutually exclusive\", in the following example.\n\n```ruby\nparams do\n  optional :beer\n  optional :wine\n  optional :juice\n  exactly_one_of :beer, :wine, :juice\nend\n```\n\nYou can rescue a `Grape::Exceptions::ValidationErrors` and respond with a custom response or turn the response into well-formatted JSON for a JSON API that separates individual parameters and the corresponding error messages. The following `rescue_from` example produces `[{\"params\":[\"beer\",\"wine\"],\"messages\":[\"are mutually exclusive\"]}]`.\n\n```ruby\nformat :json\nsubject.rescue_from Grape::Exceptions::ValidationErrors do |e|\n  error! e, 400\nend\n```\n\n`Grape::Exceptions::ValidationErrors#full_messages` returns the validation messages as an array. `Grape::Exceptions::ValidationErrors#message` joins the messages to one string.\n\nFor responding with an array of validation messages, you can use `Grape::Exceptions::ValidationErrors#full_messages`.\n```ruby\nformat :json\nsubject.rescue_from Grape::Exceptions::ValidationErrors do |e|\n  error!({ messages: e.full_messages }, 400)\nend\n```\n\nGrape returns all validation and coercion errors found by default.\nTo skip all subsequent validation checks when a specific param is found invalid, use `fail_fast: true`.\n\nThe following example will not check if `:wine` is present unless it finds `:beer`.\n```ruby\nparams do\n  required :beer, fail_fast: true\n  required :wine\nend\n```\nThe result of empty params would be a single `Grape::Exceptions::ValidationErrors` error.\n\nSimilarly, no regular expression test will be performed if `:blah` is blank in the following example.\n```ruby\nparams do\n  required :blah, allow_blank: false, regexp: \u002Fblah\u002F, fail_fast: true\nend\n```\n\n### I18n\n\nGrape supports I18n for parameter-related error messages, but will fallback to English if translations for the default locale have not been provided. See [en.yml](lib\u002Fgrape\u002Flocale\u002Fen.yml) for message keys.\n\nIn case your app enforces available locales only and :en is not included in your available locales, Grape cannot fall back to English and will return the translation key for the error message. To avoid this behaviour, either provide a translation for your default locale or add :en to your available locales.\n\nCustom validators that inherit from `Grape::Validations::Validators::Base` have access to a `translate` helper (see `Grape::Util::Translation`) and should use it instead of calling `I18n` directly. It applies the same `:en` fallback as built-in validators, defaults `scope` to `'grape.errors.messages'`, and handles interpolation without needing `format`:\n\n```ruby\n# Good — scope defaults to 'grape.errors.messages', interpolation forwarded automatically\ntranslate(:special, min: 2, max: 10)\n\n# Bad — format is unnecessary and risks conflicting with I18n reserved keys\nformat I18n.t(:special, scope: 'grape.errors.messages'), min: 2, max: 10\n```\n\nExample custom validator using an interpolated i18n message:\n\n```ruby\nclass SpecialValidator \u003C Grape::Validations::Validators::Base\n  def validate_param!(attr_name, params)\n    return if valid?(params[attr_name])\n\n    validation_error!(attr_name, translate(:special, min: 2, max: 10))\n  end\nend\n```\n\n### Custom Validation messages\n\nGrape supports custom validation messages for parameter-related and coerce-related error messages.\n\n#### `presence`, `allow_blank`, `values`, `regexp`\n\n```ruby\nparams do\n  requires :name, values: { value: 1..10, message: 'not in range from 1 to 10' }, allow_blank: { value: false, message: 'cannot be blank' }, regexp: { value: \u002F^[a-z]+$\u002F, message: 'format is invalid' }, message: 'is required'\nend\n```\n\n#### `same_as`\n\n```ruby\nparams do\n  requires :password\n  requires :password_confirmation, same_as: { value: :password, message: 'not match' }\nend\n```\n\n#### `length`\n\n```ruby\nparams do\n  requires :code, type: String, length: { is: 2, message: 'code is expected to be exactly 2 characters long' }\n  requires :str, type: String, length: { min: 5, message: 'str is expected to be at least 5 characters long' }\n  requires :list, type: [Integer], length: { min: 2, max: 3, message: 'list is expected to have between 2 and 3 elements' }\nend\n```\n\n#### `all_or_none_of`\n\n```ruby\nparams do\n  optional :beer\n  optional :wine\n  optional :juice\n  all_or_none_of :beer, :wine, :juice, message: \"all params are required or none is required\"\nend\n```\n\n#### `mutually_exclusive`\n\n```ruby\nparams do\n  optional :beer\n  optional :wine\n  optional :juice\n  mutually_exclusive :beer, :wine, :juice, message: \"are mutually exclusive cannot pass both params\"\nend\n```\n\n#### `exactly_one_of`\n\n```ruby\nparams do\n  optional :beer\n  optional :wine\n  optional :juice\n  exactly_one_of :beer, :wine, :juice, message: { exactly_one: \"are missing, exactly one parameter is required\", mutual_exclusion: \"are mutually exclusive, exactly one parameter is required\" }\nend\n```\n\n#### `at_least_one_of`\n\n```ruby\nparams do\n  optional :beer\n  optional :wine\n  optional :juice\n  at_least_one_of :beer, :wine, :juice, message: \"are missing, please specify at least one param\"\nend\n```\n\n#### `Coerce`\n\n```ruby\nparams do\n  requires :int, type: { value: Integer, message: \"type cast is invalid\" }\nend\n```\n\n#### `With Lambdas`\n\n```ruby\nparams do\n  requires :name, values: { value: -> { (1..10).to_a }, message: 'not in range from 1 to 10' }\nend\n```\n\n#### `Pass symbols for i18n translations`\n\nYou can pass a symbol if you want i18n translations for your custom validation messages.\n\n```ruby\nparams do\n  requires :name, message: :name_required\nend\n```\n```ruby\n# en.yml\n\nen:\n  grape:\n    errors:\n      format: ! '%{attributes} %{message}'\n      messages:\n        name_required: 'must be present'\n```\n\n#### Overriding Attribute Names\n\nYou can also override attribute names.\n\n```ruby\n# en.yml\n\nen:\n  grape:\n    errors:\n      format: ! '%{attributes} %{message}'\n      messages:\n        name_required: 'must be present'\n      attributes:\n        name: 'Oops! Name'\n```\nWill produce 'Oops! Name must be present'\n\n#### With Default\n\nYou cannot set a custom message option for Default as it requires interpolation `%{option1}: %{value1} is incompatible with %{option2}: %{value2}`. You can change the default error message for Default by changing the `incompatible_option_values` message key inside [en.yml](lib\u002Fgrape\u002Flocale\u002Fen.yml)\n\n```ruby\nparams do\n  requires :name, values: { value: -> { (1..10).to_a }, message: 'not in range from 1 to 10' }, default: 5\nend\n```\n\n### Using `dry-validation` or `dry-schema`\n\nAs an alternative to the `params` DSL described above, you can use a schema or `dry-validation` contract to describe an endpoint's parameters. This can be especially useful if you use the above already in some other parts of your application. If not, you'll need to add `dry-validation` or `dry-schema` to your `Gemfile`.\n\nThen call `contract` with a contract or schema defined previously:\n\n```rb\nCreateOrdersSchema = Dry::Schema.Params do\n  required(:orders).array(:hash) do\n    required(:name).filled(:string)\n    optional(:volume).maybe(:integer, lt?: 9)\n  end\nend\n\n# ...\n\ncontract CreateOrdersSchema\n```\n\nor with a block, using the [schema definition syntax](https:\u002F\u002Fdry-rb.org\u002Fgems\u002Fdry-schema\u002F1.13\u002F#quick-start):\n\n```rb\ncontract do\n  required(:orders).array(:hash) do\n    required(:name).filled(:string)\n    optional(:volume).maybe(:integer, lt?: 9)\n  end\nend\n```\n\nThe latter will define a coercing schema (`Dry::Schema.Params`). When using the former approach, it's up to you to decide whether the input will need coercing.\n\nThe `params` and `contract` declarations can also be used together in the same API, e.g. to describe different parts of a nested namespace for an endpoint.\n\n## Headers\n\n### Request\nRequest headers are available through the `headers` helper or from `env` in their original form.\n\n```ruby\nget do\n  error!('Unauthorized', 401) unless headers['Secret-Password'] == 'swordfish'\nend\n```\n\n```ruby\nget do\n  error!('Unauthorized', 401) unless env['HTTP_SECRET_PASSWORD'] == 'swordfish'\nend\n```\n\n#### Header Case Handling\n\nThe above example may have been requested as follows:\n\n``` shell\ncurl -H \"secret_PassWord: swordfish\" ...\n```\n\nThe header name will have been normalized for you.\n\n- In the `header` helper names will be coerced into a downcased kebab case as `secret-password` if using Rack 3.\n- In the `header` helper names will be coerced into a capitalized kebab case as `Secret-PassWord` if using Rack \u003C 3.\n- In the `env` collection they appear in all uppercase, in snake case, and prefixed with 'HTTP_' as `HTTP_SECRET_PASSWORD`\n\nThe header name will have been normalized per HTTP standards defined in [RFC2616 Section 4.2](https:\u002F\u002Fwww.w3.org\u002FProtocols\u002Frfc2616\u002Frfc2616-sec4.html#sec4.2) regardless of what is being sent by a client.\n\n### Response\n\nYou can set a response header with `header` inside an API.\n\n```ruby\nheader 'X-Robots-Tag', 'noindex'\n```\n\nWhen raising `error!`, pass additional headers as arguments. Additional headers will be merged with headers set before `error!` call.\n\n```ruby\nerror! 'Unauthorized', 401, 'X-Error-Detail' => 'Invalid token.'\n```\n\n## Routes\n\nTo define routes you can use the `route` method or the shorthands for the HTTP verbs. To define a route that accepts any route set to `:any`.\nParts of the path that are denoted with a colon will be interpreted as route parameters.\n\n```ruby\nroute :get, 'status' do\nend\n\n# is the same as\n\nget 'status' do\nend\n\n# is the same as\n\nget :status do\nend\n\n# is NOT the same as\n\nget ':status' do # this makes params[:status] available\nend\n\n# This will make both params[:status_id] and params[:id] available\n\nget 'statuses\u002F:status_id\u002Freviews\u002F:id' do\nend\n```\n\nTo declare a namespace that prefixes all routes within, use the `namespace` method. `group`, `resource`, `resources` and `segment` are aliases to this method. Any endpoints within will share their parent context as well as any configuration done in the namespace context.\n\nThe `route_param` method is a convenient method for defining a parameter route segment. If you define a type, it will add a validation for this parameter.\n\n```ruby\nroute_param :id, type: Integer do\n  get 'status' do\n  end\nend\n\n# is the same as\n\nnamespace ':id' do\n  params do\n    requires :id, type: Integer\n  end\n\n  get 'status' do\n  end\nend\n```\n\nOptionally, you can define requirements for your named route parameters using regular expressions on namespace or endpoint. The route will match only if all requirements are met.\n\n```ruby\nget ':id', requirements: { id: \u002F[0-9]*\u002F } do\n  Status.find(params[:id])\nend\n\nnamespace :outer, requirements: { id: \u002F[0-9]*\u002F } do\n  get :id do\n  end\n\n  get ':id\u002Fedit' do\n  end\nend\n```\n\n## Helpers\n\nYou can define helper methods that your endpoints can use with the `helpers` macro by either giving a block or an array of modules.\n\n```ruby\nmodule StatusHelpers\n  def user_info(user)\n    \"#{user} has statused #{user.statuses} status(s)\"\n  end\nend\n\nmodule HttpCodesHelpers\n  def unauthorized\n    401\n  end\nend\n\nclass API \u003C Grape::API\n  # define helpers with a block\n  helpers do\n    def current_user\n      User.find(params[:user_id])\n    end\n  end\n\n  # or mix in an array of modules\n  helpers StatusHelpers, HttpCodesHelpers\n\n  before do\n    error!('Access Denied', unauthorized) unless current_user\n  end\n\n  get 'info' do\n    # helpers available in your endpoint and filters\n    user_info(current_user)\n  end\nend\n```\n\nYou can define reusable `params` using `helpers`.\n\n```ruby\nclass API \u003C Grape::API\n  helpers do\n    params :pagination do\n      optional :page, type: Integer\n      optional :per_page, type: Integer\n    end\n  end\n\n  desc 'Get collection'\n  params do\n    use :pagination # aliases: includes, use_scope\n  end\n  get do\n    Collection.page(params[:page]).per(params[:per_page])\n  end\nend\n```\n\nYou can also define reusable `params` using shared helpers.\n\n```ruby\nmodule SharedParams\n  extend Grape::API::Helpers\n\n  params :period do\n    optional :start_date\n    optional :end_date\n  end\n\n  params :pagination do\n    optional :page, type: Integer\n    optional :per_page, type: Integer\n  end\nend\n\nclass API \u003C Grape::API\n  helpers SharedParams\n\n  desc 'Get collection.'\n  params do\n    use :period, :pagination\n  end\n\n  get do\n    Collection\n      .from(params[:start_date])\n      .to(params[:end_date])\n      .page(params[:page])\n      .per(params[:per_page])\n  end\nend\n```\n\nHelpers support blocks that can help set default values. The following API can return a collection sorted by `id` or `created_at` in `asc` or `desc` order.\n\n```ruby\nmodule SharedParams\n  extend Grape::API::Helpers\n\n  params :order do |options|\n    optional :order_by, type: Symbol, values: options[:order_by], default: options[:default_order_by]\n    optional :order, type: Symbol, values: %i(asc desc), default: options[:default_or","Grape 是一个用于创建 RESTful API 的 Ruby 框架。它提供了简洁的 DSL（领域特定语言），能够轻松地开发遵循 REST 架构风格的 API，并且支持多种格式、子域名\u002F前缀限制、内容协商、版本控制等常见功能。Grape 可以独立运行于 Rack 上，也可以与 Rails 或 Sinatra 等现有 Web 应用框架结合使用。适合需要快速搭建稳定、可维护的 RESTful 服务的应用场景，特别是在已有 Ruby 技术栈的项目中集成 API 功能时尤为适用。","2026-06-11 03:13:34","top_language"]