{
  "$schema": "../rpc-schema-draft.json",
  "type": "object",
  "rpc": "fetchinvoice",
  "title": "Command for fetch an invoice for an offer",
  "description": [
    "The **fetchinvoice** RPC command contacts the issuer of an *offer* to get an actual invoice that can be paid. It highlights any changes between the offer and the returned invoice.",
    "",
    "If **fetchinvoice-noconnect** is not specified in the configuation, it will connect to the destination in the (currently common!) case where it cannot find a route which supports `option_onion_messages`."
  ],
  "request": {
    "required": [
      "offer"
    ],
    "additionalProperties": false,
    "properties": {
      "offer": {
        "type": "string",
        "description": [
          "Offer string to get an actual invoice that can be paid."
        ]
      },
      "amount_msat": {
        "type": "msat",
        "description": [
          "Required if the offer does not specify an amount at all, otherwise it is optional (but presumably if you set it to less than the offer, you will get an error from the issuer)."
        ]
      },
      "quantity": {
        "type": "u64",
        "description": [
          "Required if the offer specifies quantity_max, otherwise it is not allowed."
        ]
      },
      "recurrence_counter": {
        "type": "u64",
        "description": [
          "Required if the offer specifies recurrence, otherwise it is not allowed. recurrence_counter should first be set to 0, and incremented for each successive invoice in a given series."
        ]
      },
      "recurrence_start": {
        "type": "number",
        "description": [
          "Indicates what period number to start at (usually 0)."
        ]
      },
      "recurrence_label": {
        "type": "string",
        "description": [
          "Required if recurrence_counter is set, and otherwise is not allowed. It must be the same as prior fetchinvoice calls for the same recurrence, as it is used to link them together."
        ]
      },
      "timeout": {
        "type": "number",
        "description": [
          "If we don't get a reply before this we fail (default, 60 seconds)."
        ]
      },
      "payer_note": {
        "type": "string",
        "description": [
          "To ask the issuer to include in the fetched invoice."
        ]
      },
      "payer_metadata": {
        "type": "string",
        "description": [
          "Derive the payer_id from the specified payer_metadata. Please be sure that `payer_metdata` can not be derived by anyone, so put some secret into it."
        ],
        "added": "v24.11"
      },
      "bip353": {
        "type": "string",
        "description": [
          "BIP353 string (optionally with ₿) indicating where we fetched the offer from"
        ],
        "added": "v25.02"
      },
      "dev_reply_path": {
        "hidden": true
      },
      "dev_path_use_scidd": {
        "hidden": true
      }
    }
  },
  "response": {
    "required": [
      "invoice",
      "changes"
    ],
    "additionalProperties": false,
    "properties": {
      "invoice": {
        "type": "string",
        "description": [
          "The BOLT12 invoice we fetched."
        ]
      },
      "changes": {
        "type": "object",
        "description": [
          "Summary of changes from offer."
        ],
        "additionalProperties": false,
        "required": [],
        "properties": {
          "description_appended": {
            "type": "string",
            "description": [
              "Extra characters appended to the *description* field."
            ]
          },
          "description": {
            "type": "string",
            "description": [
              "A completely replaced *description* field."
            ]
          },
          "vendor_removed": {
            "type": "string",
            "description": [
              "The *vendor* from the offer, which is missing in the invoice."
            ]
          },
          "vendor": {
            "type": "string",
            "description": [
              "A completely replaced *vendor* field."
            ]
          },
          "amount_msat": {
            "type": "msat",
            "description": [
              "The amount, if different from the offer amount multiplied by any *quantity* (or the offer had no amount, or was not in BTC)."
            ]
          }
        }
      },
      "next_period": {
        "type": "object",
        "description": [
          "Only for recurring invoices if the next period is under the *recurrence_limit*."
        ],
        "additionalProperties": false,
        "required": [
          "counter",
          "starttime",
          "endtime",
          "paywindow_start",
          "paywindow_end"
        ],
        "properties": {
          "counter": {
            "type": "u64",
            "description": [
              "The index of the next period to fetchinvoice."
            ]
          },
          "starttime": {
            "type": "u64",
            "description": [
              "UNIX timestamp that the next period starts."
            ]
          },
          "endtime": {
            "type": "u64",
            "description": [
              "UNIX timestamp that the next period ends."
            ]
          },
          "paywindow_start": {
            "type": "u64",
            "description": [
              "UNIX timestamp of the earliest time that the next invoice can be fetched."
            ]
          },
          "paywindow_end": {
            "type": "u64",
            "description": [
              "UNIX timestamp of the latest time that the next invoice can be fetched."
            ]
          }
        }
      }
    }
  },
  "errors": [
    "The following error codes may occur:",
    "",
    "- -1: Catchall nonspecific error.",
    "- 1002: Offer has expired.",
    "- 1003: Cannot find a route to the node making the offer.",
    "- 1004: The node making the offer returned an error message.",
    "- 1005: We timed out trying to fetch an invoice."
  ],
  "author": [
    "Rusty Russell [rusty@rustcorp.com.au](mailto:rusty@rustcorp.com.au) is mainly responsible."
  ],
  "see_also": [
    "lightning-sendinvoice(7)",
    "lightning-pay(7)"
  ],
  "resources": [
    "Main web site: [https://github.com/ElementsProject/lightning](https://github.com/ElementsProject/lightning)"
  ],
  "examples": [
    {
      "request": {
        "id": "example:fetchinvoice#1",
        "method": "fetchinvoice",
        "params": {
          "offer": "lno1qgsq000bolt210002100021000210002100021000210002100021000210002100021000210002100021000210002100021000210002100021000210002100021000",
          "payer_note": "Thanks for the fish!"
        }
      },
      "response": {
        "invoice": "lni1qqg0qe01010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101",
        "changes": {}
      }
    },
    {
      "request": {
        "id": "example:fetchinvoice#2",
        "method": "fetchinvoice",
        "params": {
          "offer": "lno1qgsq000bolt220002200022000220002200022000220002200022000220002200022000220002200022000220002200022000220002200022000220002200022000",
          "amount_msat": 2000000,
          "quantity": 2
        }
      },
      "response": {
        "invoice": "lni1qqg0qe02020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202",
        "changes": {}
      }
    }
  ]
}
