--- title: Answer API (open beta) slug: p3ob0090 canonical_url: https://docs.coveo.com/en/p3ob0090/ collection: apis source_format: adoc --- # Answer API (open beta) You can generate answers based on your Coveo [index](https://docs.coveo.com/en/204/) using [Relevance Generative Answering (RGA)](https://docs.coveo.com/en/nbtb6010/) in interfaces built with one of the Coveo Search UI libraries ([Atomic](https://docs.coveo.com/en/atomic/latest/), [Headless](https://docs.coveo.com/en/headless/latest/), [Quantic](https://docs.coveo.com/en/quantic/latest/)) or in a Coveo builder ([Hosted search page builder](https://docs.coveo.com/en/1656/), [In-Product Experience (IPX) builder](https://docs.coveo.com/en/3160/), [Hosted Insight Panel builder](https://docs.coveo.com/en/m68b0168/)). Such interfaces don't need to call the Answer API directly, as they already have the capability to generate answers using the Coveo Search API. The Answer API allows you to add [RGA](https://docs.coveo.com/en/nbtb6010/)-generated answers to any custom search interface. > **Note** > > To [generate the answer](https://docs.coveo.com/en/n9de0370#answer-generation), [RGA](https://docs.coveo.com/en/nbtb6010/) uses a [third-party generative LLM](https://docs.coveo.com/en/pb7d0331/) that's hosted on an external foundation model service server. Additionally, leveraging the Answer API in an interface built with a Coveo Search UI library or in a Coveo builder allows you to benefit from the [Answer Manager in the Coveo Knowledge Hub](https://docs.coveo.com/en/p58d0270/). For such a use case, you'll need to perform additional steps specified [down below](#additional-steps). This article focuses on how to directly call the Answer API to generate answers from the Coveo index. It also includes tips on how to enable the Answer API in interfaces built with a Coveo Search UI library or in a Coveo builder. ## Limitations The `/generate` endpoint doesn't currently provide debugging information if the Answer API can't generate an answer. In such a case, the response payload looks like this: ```json { "payloadType": "genqa.endOfStreamType", "payload": "{\"answerGenerated\":false}", "finishReason": "COMPLETED", "errorMessage": null, "statusCode": null } ``` ## Prerequisites . Make sure you have the Coveo Knowledge Hub activated in your organization. > **Important** > > The Coveo Knowledge Hub is currently available as a beta to early-access customers only. > Contact your Customer Success Manager for early access to this feature. . An access token: Either an [API key](https://docs.coveo.com/en/105/) that was created using the **Anonymous search** [template](https://docs.coveo.com/en/1718#api-key-templates) or a [search token](https://docs.coveo.com/en/56/) that grants the **Allowed** [access level](https://docs.coveo.com/en/2818/) on the [**Execute Queries**](https://docs.coveo.com/en/1707#execute-queries-domain) [domain](https://docs.coveo.com/en/2819/). > **Tip** > > If you're querying the Answer API from an interface built with a Coveo Search UI library or in a Coveo builder, make sure it's [RGA-enabled](https://docs.coveo.com/en/nb6a0037/). ## [[additional-steps]]Activate the Answer API To start using the Answer API, perform the following steps first. . [Create an answer configuration in the Coveo Knowledge Hub](https://docs.coveo.com/en/p5db9314/). When performing this step, copy and save the **Configuration ID** of the newly created answer configuration as you will need it to make requests to the Answer API. . (Optional) Enable [rich text formatting](https://docs.coveo.com/en/nb6a0104#rich-text-formatting) for the generated answers. > **Notes** > > * Rich text formatting is enabled by default for new [RGA](https://docs.coveo.com/en/nbtb6010/) model associations. > > * To use rich text formatting, you must also add the `contentFormat` parameter in the [request payload](#request-payload). . If you're calling the Answer API from a search page that calls the Search API, or if your interface is built with a Coveo Search UI library or in a Coveo builder: .. [Associate the created configuration with your RGA-enabled search interface](https://docs.coveo.com/en/p5db9314#associate-an-answer-configuration-with-a-search-interface). .. As your [RGA](https://docs.coveo.com/en/nbtb6010/)-enabled search interface is associated with an answer configuration, this may cause duplicate [generative queries per month (GQPM)](https://docs.coveo.com/en/nc5e0379/). To avoid this, see [Prevent duplicate GQPM when using the Answer Manager](https://docs.coveo.com/en/nb6a0104#prevent-duplicate-gqpm-when-using-the-answer-manager). ## Request endpoint for generating answers To generate an answer, send a POST request to the following endpoint. The example below shows how to generate an answer from the Coveo index: ```http POST https://.org.coveo.com/rest/organizations//answer/v1/configs//generate HTTP/1.1 Content-Type: application/json Accept: application/json, text/event-stream Authorization: Bearer ``` Where: * `` is your [Coveo organization ID](https://docs.coveo.com/en/n1ce5273/) * `` is the configuration ID that you retrieved when you created the answer configuration in the Coveo Knowledge Hub. * `` is your [access token](https://docs.coveo.com/en/123/). ## Request payload ```json { "q": "Which gloves are better for autumn?", "searchHub": "sports", <1> "pipeline": "Sport goods pipeline", "pipelineRuleParameters": { "mlGenerativeQuestionAnswering": { "responseFormat": { "contentFormat": ["text/markdown", "text/plain"] <2> }, "citationsFieldToInclude": [ <3> "firstFieldForCitations", "secondFieldForCitations" ] } }, // ... } ``` <1> Specify this parameter only if you're using a temporary access token that's not associated with any specific search hub. > **Note** > > If you're using an API key or a search token which is set up to use with a specific search hub, you can omit this parameter. > However, you must specify the search hub as a condition in your query pipeline configuration. <2> Makes sure the response is in the desired format. The possible values are `"text/markdown" and "text/plain"`. It's recommended to provide both values to ensure the answer displays in plain text if markdown formatting fails. <3> Specifies the fields to include in the citations. The fields must be present in the documents returned by the query. For the full list of possible parameters, see the [reference documentation](https://docs.coveo.com/en/3430#tag/Answer). For example, in addition to the above, you might pass the following: ```json { // ... "context": {"userAgeRange":"25-35","userRoles":["PremiumCustomer","ProductReviewer"]}, <1> "locale": "en-US", <2> } ``` <1> Lets you define context you can leverage in query pipeline conditions. <2> Specifies the locale to use for the answer generation, when multiple locales are supported. ## Response payload with the generated answer The Answer API uses server-sent events to fulfill your requests. Each event stream starts with a `headerMessageType` event, then sends one or more `messageType` events with the answer text, then sends a `citationsType` event with the citations, and finally ends with an `endOfStreamType` event. ```json { "payloadType": "genqa.headerMessageType", "payload": "{\"answerStyle\":\"default\",\"contentFormat\":\"text/markdown\"}", "finishReason": null, "errorMessage": null, "statusCode": null } { "payloadType": "genqa.messageType", "payload": "{\"textDelta\":\"### Autumn gloves\n\nThe following types of gloves are better suited for...,\",\"padding\":\"123456789\"}", "finishReason": null, "errorMessage": null, "statusCode": null } { "payloadType": "genqa.citationsType", "payload": "{\"citations\":[{\"id\":\"42.34281$https://example.com/articles/gloves-8:3\",\"title\":\"Sport gloves classification\",\"uri\":\"https://example.com/articles/gloves\",\"clickUri\":\"https://example.com/articles/gloves\",\"permanentid\":\"37a2594e9158310706d4b85cde9299b519653427f9b095c53332b9342577\",\"text\":\"lorem ipsum gloves\",\"source\":\"example.com\"}]}", "finishReason": null, "errorMessage": null, "statusCode": null } { "payloadType": "genqa.endOfStreamType", "payload": "{\"answerGenerated\":true}", "finishReason": "COMPLETED", "errorMessage": null, "statusCode": null } ``` ## See the Answer API in action Your search interface should have a key action that triggers the generation of an answer, it might be making a search, asking a chatbot a question, and so on. You can check if the Answer API is working properly by triggering that action and seeing the `generate` request on the network tab of your browser's developer tools. ![A "generate" request to the Answer API on the Network tab of a browser | Coveo Platform](https://docs.coveo.com/en/assets/images/leverage-machine-learning/generate.png) ## API Reference For more details, see the [Answer API reference](https://docs.coveo.com/en/3430#tag/Answer).