> ## Documentation Index
> Fetch the complete documentation index at: https://docs.appsignal.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Grape

export const VersionRequirements = ({versions = []}) => {
  if (!Array.isArray(versions) || versions.length === 0) {
    return null;
  }
  const boundaries = {
    upper: " or lower",
    exact: "",
    lower: " or higher"
  };
  const containerStyle = {
    marginTop: "0.5rem",
    marginBottom: "1.5rem"
  };
  const lineStyle = {
    display: "block",
    margin: "0 0 0.35rem 0",
    fontSize: "0.875rem",
    lineHeight: "1.4"
  };
  const pillBaseStyle = {
    display: "inline-block",
    padding: "0.1em 0.4em",
    borderRadius: "0.25rem",
    border: "1px solid",
    fontFamily: "ui-monospace, SFMono-Regular, Menlo, monospace",
    fontSize: "0.85em",
    fontWeight: 500
  };
  const pillColors = {
    "AppSignal for Elixir": {
      background: "#f3e8ff",
      borderColor: "#e9d5ff",
      color: "#9333ea"
    },
    "AppSignal for Front-end": {
      background: "#fef9c3",
      borderColor: "#fde68a",
      color: "#ca8a04"
    },
    "AppSignal for Go": {
      background: "#ccfbf1",
      borderColor: "#99f6e4",
      color: "#0d9488"
    },
    "AppSignal for JavaScript": {
      background: "#fef9c3",
      borderColor: "#fde68a",
      color: "#ca8a04"
    },
    "AppSignal for Node.js": {
      background: "#dcfce7",
      borderColor: "#bbf7d0",
      color: "#16a34a"
    },
    "AppSignal for Python": {
      background: "#dbeafe",
      borderColor: "#bfdbfe",
      color: "#2563eb"
    },
    "AppSignal for Ruby": {
      background: "#fee2e2",
      borderColor: "#fecaca",
      color: "#dc2626"
    },
    "AppSignal for Rust": {
      background: "#ffedd5",
      borderColor: "#fed7aa",
      color: "#ea580c"
    }
  };
  const defaultPillColor = {
    background: "#f4f4f5",
    borderColor: "#e4e4e7",
    color: "#52525b"
  };
  const getPillStyle = name => ({
    ...pillBaseStyle,
    ...pillColors[name] || defaultPillColor
  });
  const getBoundText = bound => {
    return boundaries[bound] || boundaries.lower;
  };
  return <div style={containerStyle}>
      {versions.map((v, i) => <p key={`${v.name}-${v.version}-${v.bound || "lower"}-${i}`} style={lineStyle}>
          This feature requires{" "}
          <code style={getPillStyle(v.name)}>{v.name}</code> version {v.version}
          {getBoundText(v.bound)}.
        </p>)}
    </div>;
};

export const Compatibility = ({versions = [], label = "Available in"}) => {
  if (!Array.isArray(versions) || versions.length === 0) {
    return null;
  }
  const defaultPillStyle = {
    borderColor: "#d4d4d8",
    background: "#f4f4f5",
    color: "#3f3f46"
  };
  const pillStyles = {
    "AppSignal for Elixir": {
      background: "#f3e8ff",
      borderColor: "#d8b4fe",
      color: "#6b21a8"
    },
    "AppSignal for Front-end": {
      background: "#fef9c3",
      borderColor: "#fde047",
      color: "#854d0e"
    },
    "AppSignal for Go": {
      background: "#ccfbf1",
      borderColor: "#5eead4",
      color: "#115e59"
    },
    "AppSignal for JavaScript": {
      background: "#fef9c3",
      borderColor: "#fde047",
      color: "#854d0e"
    },
    "AppSignal for Node.js": {
      background: "#dcfce7",
      borderColor: "#86efac",
      color: "#166534"
    },
    "AppSignal for Python": {
      background: "#dbeafe",
      borderColor: "#93c5fd",
      color: "#1e40af"
    },
    "AppSignal for Ruby": {
      background: "#fee2e2",
      borderColor: "#fca5a5",
      color: "#991b1b"
    },
    "AppSignal for Rust": {
      background: "#ffedd5",
      borderColor: "#fdba74",
      color: "#9a3412"
    }
  };
  const getPillStyle = name => ({
    ...defaultPillStyle,
    ...pillStyles[name] || ({})
  });
  return <div className="not-prose my-4 rounded-lg border border-zinc-200 bg-zinc-50 px-4 py-3 text-sm dark:border-white/10 dark:bg-white/5">
      <div className="flex flex-wrap items-center gap-x-2 gap-y-1">
        <span className="font-semibold text-zinc-700 dark:text-zinc-200">
          {label}:
        </span>
        {versions.map((v, i) => <span key={`${v.name}-${v.version}-${i}`} className="inline-flex items-center gap-1 rounded-full border px-2 py-0.5 text-xs font-medium" style={getPillStyle(v.name)}>
            <span>{v.name}</span>
            <span className="opacity-70">
              {v.version}
              {v.exact ? "" : "+"}
            </span>
          </span>)}
      </div>
    </div>;
};

<VersionRequirements
  versions={[
{ name: "AppSignal for Ruby", version: "1.1" },
{ name: "Grape", version: "1.0" }
]}
/>

[Grape](https://www.ruby-grape.org/) applications are officially supported. Instrumenting Grape applications requires some manual setup. Follow the installation steps in AppSignal, starting by clicking 'Add app' on the [accounts screen](https://appsignal.com/accounts).

<Note>
  Is your application using a combination of Rails, Grape, Hanami, Padrino or Sinatra? Follow our guide for [instrumenting multiple Rack applications](/ruby/integrations/rack-libraries).
</Note>

## Installation

<Tip>
  This installation guide is for Ruby gem 3.12 and newer. For applications using
  an older AppSignal for Ruby gem, follow our [legacy installation
  guide](#legacy-installation).
</Tip>

After [installing and configuring the AppSignal gem](/ruby/installation), add the AppSignal integration after the Grape app is loaded.

The Grape integration is required from the AppSignal Ruby gem, and the event and instrumentation middlewares are added in the following code examples.

<CodeGroup>
  ```ruby Ruby theme={null}
  # config.ru
  require "appsignal" # Add this require

  # Load the Grape integration
  Appsignal.load(:grape)

  # Add the AppSignal Rack event middleware
  # AppSignal for Ruby gem 4.7+ required
  use Appsignal::Rack::EventMiddleware

  # Require the app
  require_relative "api"

  # Start AppSignal
  Appsignal.start

  # Start the app
  run Acme::API
  ```
</CodeGroup>

The next step is to add the AppSignal middleware to the application's base API class. It will report any unhandled exceptions from the application.

<CodeGroup>
  ```ruby Ruby theme={null}
  # api.rb

  module Acme
    class API < Grape::API
      # Add this line
      insert_before Grape::Middleware::Error, Appsignal::Rack::GrapeMiddleware # Include this middleware

      # ...
    end
  end
  ```
</CodeGroup>

### Legacy installation

Applications using AppSignal for Ruby gem 3.11 or older, follow these steps to install AppSignal in Grape applications.

The Grape integration is required from the AppSignal Ruby gem, and the event handler and instrumentation middlewares are added in the following code examples.

<CodeGroup>
  ```ruby Ruby theme={null}
  # config.ru
  require "appsignal/integrations/grape" # Add this require

  # Configure AppSignal
  Appsignal.config = Appsignal::Config.new(
    Dir.pwd,        # The root of your app
    ENV["RACK_ENV"] # The environment of your app (development/production)
  )
  # Start AppSignal
  Appsignal.start_logger # Not required in Ruby gem 3.9.3+
  Appsignal.start

  # Add the AppSignal Rack EventHandler
  # AppSignal for Ruby gem 3.8+ required
  use ::Rack::Events, [Appsignal::Rack::EventHandler.new]

  # Require the app
  require_relative "api"

  # Start the app
  run Acme::API
  ```
</CodeGroup>

The next step is to add the AppSignal middleware to the application's base API class. It will report any unhandled exceptions from the application.

<CodeGroup>
  ```ruby Ruby theme={null}
  # api.rb

  module Acme
    class API < Grape::API
      # Add this line
      insert_before Grape::Middleware::Error, Appsignal::Grape::Middleware # Include this middleware

      # ...
    end
  end
  ```
</CodeGroup>

## Mounted Grape apps

<Compatibility versions={[{ name: "AppSignal for Ruby", version: "3.9.3" }]} />

Mounting Grape applications on Ruby on Rails applications is supported. The Ruby gem needs to be a recent version to properly instrument requests for mounted Grape apps.

## Ignoring errors

<Compatibility versions={[{ name: "AppSignal for Ruby", version: "2.10.5" }]} />

To ignore a specific Grape error, set the `grape.skip_appsignal_error` flag in the request environment. Setting this flag to `true` will instruct AppSignal not to report any errors during the request.

Use this flag only if you need to dynamically ignore errors from a Grape application. Specify error classes in the [`ignore_errors` option](/guides/filter-data/ignore-errors) to ignore them for the entire application. For more information on muting notifications for specific errors see our [notification settings documentation](/application/notification-settings).

<CodeGroup>
  ```ruby Ruby theme={null}
  get "/" do
    env["grape.skip_appsignal_error"] = true # Add this line to an endpoint or callback
    raise "uh oh" # Example error, don't copy this
  end
  ```
</CodeGroup>

## Reporting errors from `rescue_from`

If an error is rescued in the app using `rescue_from,` AppSignal will not receive and track it. To still report the error, call `Appsignal.report_error` in the `rescue_from` block. (Use the `Appsignal.set_error` helper when using Ruby gem version 3 or older.)

<CodeGroup>
  ```ruby Ruby theme={null}
  class Api < ::Grape::API
    insert_before Grape::Middleware::Error, Appsignal::Grape::Middleware

    format :json

    rescue_from :all do |error|
      Appsignal.report_error(error)
      error!({ :error => "error message" }.to_json, 500)
    end
  end
  ```
</CodeGroup>
