# Trade RFS

## Goal

Execute a trade in the RFS workflow, from login and requesting the stream to downloading the done trade.

## Prerequisites

- Your REST API app.
- Integral API login with trading permission.
- Your organization provisioned with at least one provider who streams RFS prices.


## Steps


```mermaid
flowchart TD
  LOGIN(Step 1: Login) --> QUOTES(Step 2: Request quotes)
  QUOTES --> |Request status = REQUEST_RECEIVED| REQUESTSUCCESS(Successful request)
  REQUESTSUCCESS --> QUERYREQUEST(Step 3: Query request)
  QUERYREQUEST --> |Poll request status| QUERYREQUEST
  QUERYREQUEST --> ACCEPTQUOTE(Step 4: Accept quote)
  ACCEPTQUOTE --> |202 Success| QUERYFORTRADE(Step 5: Query quote request for trades)
  click LOGIN "#step-1-login"
  click QUOTES "#step-2-request-quotes"
  click QUERYREQUEST "#step-3-query-request"
  click ACCEPTQUOTE "#step-4-accept-quote"
  click QUERYFORTRADE "#step-5-query-quote-request-for-trades"
```

### Step 1: Login

Use the [Login and get token](/openapi/integral-api-reference/rest/authentication-api/login) endpoint.

See the related [Login](/developer-portal/tutorials/tutorialauth) tutorial.

Your access token is in `SSO_TOKEN` of the response header. Your token is valid for limited time.

Pass the `SSO_TOKEN` cookie value with all of your subsequent API requests.

### Step 2: Request quotes

Use the [Request quote](/openapi/integral-api-reference/rest/rfsrfq/quoterequest) endpoint.

### Step 3: Query request

You can query by two different IDs:

* The `clOrderId` assigned by you and sent with your request (the [Query quote request (client ID)](/openapi/integral-api-reference/rest/rfsrfq/queryrequestclientid) endpoint).
* The `requestId` attribute of the JSON response in the next step (the [Query quote request (server ID)](/openapi/integral-api-reference/rest/rfsrfq/queryrequestserverid) endpoint).


You must query your quote request for its status and to get quotes. After the initial success/fail response, status updates and quotes are not pushed to you.

h4
Request state
Your request can have any one of the following states:

* `REQUEST_RECEIVED`: request for quote is received for processing, quoting has not started.
* `QUOTE`: a quote in response to your request for quote.
* `REQUEST_DECLINED`: request declined.
* `REQUEST_EXPIRED`: request expired and inactive.
* `REQUEST_WITHDRAWN`: request was canceled by you.
* `REQUEST_ACCEPTED`: request to trade a quote is accepted.
* `TRADE_REJECTED`: request to trade a quote rejected by one or more liquidity providers.
* `TRADE_PENDING`: trades pending in response to your request to trade a quote.
* `TRADE_VERIFIED`: trades executed in response to your request to trade a quote.


Request states progress and relate to each other in the following ways:

div
Request for quote state transitions

```mermaid
stateDiagram-v2
  [*] --> REQUEST_RECEIVED
  REQUEST_RECEIVED --> REQUEST_DECLINED
  REQUEST_DECLINED --> [*]
  REQUEST_RECEIVED --> REQUEST_EXPIRED
  REQUEST_EXPIRED --> [*]
  REQUEST_RECEIVED --> QUOTE
  QUOTE --> REQUEST_EXPIRED
  REQUEST_RECEIVED --> REQUEST_WITHDRAWN
  REQUEST_WITHDRAWN --> [*]
  QUOTE --> REQUEST_WITHDRAWN
  QUOTE --> REQUEST_ACCEPTED
  REQUEST_ACCEPTED --> TRADE_VERIFIED
  TRADE_VERIFIED --> [*]
  REQUEST_ACCEPTED --> TRADE_PENDING
  TRADE_PENDING --> [*]
  REQUEST_ACCEPTED --> TRADE_REJECTED
  TRADE_REJECTED --> [*]
```

#### Query by client ID

#### Query by server ID

### Step 4: Accept quote

Use the [Accept quote](/openapi/integral-api-reference/rest/rfsrfq/acceptquote) endpoint.

The diagram shows the values returned in the `status` field in the response.

div
Accept quote state transition

```mermaid
sequenceDiagram
  Client app->>Integral: Request to trade quote
  Integral->>Client app: State=QUOTE
  Client app->>Integral: Accept quote
  Integral->>Client app: State=REQUEST_ACCEPTED
  Client app->>Integral: Query quote request for trade state
  alt Verified trade
  Integral->>Client app: State=TRADE_VERIFIED
  Note over Client app,Integral: Response from Integral<br/>with done trades
  else Pending trade
  Integral->>Client app: State=TRADE_PENDING
  Client app->>Integral: Query quote request for trade state
  else Rejected trade
  Integral->>Client app: State=TRADE_REJECTED
  end
```

### Step 5: Query quote request for trades

You must query your original quote request to get the done trades. After a success response from your accept quote, status updates and trades are not pushed to you.

You must query your original quote request to get the done trades.

The `event` attribute value `TRADE_VERIFIED` on the response indicates that trades were done by your request to trade a quote. The `trades` array on the response contains the done trades.

You can query by two different IDs:

* The `clOrderId` assigned by you and sent with your request (the [Query quote request (client ID)](/openapi/integral-api-reference/rest/rfsrfq/queryrequestclientid) endpoint).
* The `requestId` attribute of the JSON response in the next step (the [Query quote request (server ID)](/openapi/integral-api-reference/rest/rfsrfq/queryrequestserverid) endpoint).


#### Query by client ID

#### Query by server ID