# Trade the one-time code from a single sign-on for a session. The answer’s set-auth-token header holds the token.

```http
POST /api/sso/exchange
```

## Request body

- **code** `string`: At most 200 characters
- **verifier** `string`: At most 200 characters

## Response

**200**: It worked

- **user** `User` (required)
  - **id** `string` (required): At most 300 characters
  - **email** `string` (required): At most 300 characters
  - **name** `string` (required): At most 300 characters
  - **image** `string | null`
  - **twoStep** `boolean`: Sign-in asks for a code from an authenticator app too

Other answers:

- **4XX**: Refused, with a message to show the person

## Example request

````tabs
tab: cURL
```bash
curl -X POST 'https://your-hatch-server/api/sso/exchange' \
  -H 'Content-Type: application/json' \
  -d '{
  "code": "string",
  "verifier": "string"
}'
```

tab: JavaScript
```js
const res = await fetch("https://your-hatch-server/api/sso/exchange", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    "code": "string",
    "verifier": "string"
  }),
})
const data = await res.json()
```

tab: Python
```python
import requests

res = requests.request("POST", "https://your-hatch-server/api/sso/exchange", headers={"Content-Type": "application/json"}, json={
    "code": "string",
    "verifier": "string",
})
data = res.json()
```

tab: Go
```go
package main

import (
	"fmt"
	"io"
	"net/http"
	"strings"
)

func main() {
	body := strings.NewReader(`{
	  "code": "string",
	  "verifier": "string"
	}`)
	req, err := http.NewRequest("POST", "https://your-hatch-server/api/sso/exchange", body)
	if err != nil {
		panic(err)
	}
	req.Header.Set("Content-Type", "application/json")
	res, err := http.DefaultClient.Do(req)
	if err != nil {
		panic(err)
	}
	defer res.Body.Close()
	data, _ := io.ReadAll(res.Body)
	fmt.Println(string(data))
}
```

tab: Ruby
```ruby
require "net/http"
require "json"

uri = URI('https://your-hatch-server/api/sso/exchange')
req = Net::HTTP::Post.new(uri)
req['Content-Type'] = 'application/json'
req.body = JSON.generate({
  'code' => 'string',
  'verifier' => 'string'
})

res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https") do |http|
  http.request(req)
end
puts res.body
```

tab: PHP
```php
<?php

$ch = curl_init('https://your-hatch-server/api/sso/exchange');
curl_setopt_array($ch, [
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => [
        'Content-Type: application/json',
    ],
    CURLOPT_POSTFIELDS => '{
      "code": "string",
      "verifier": "string"
    }',
]);

$response = curl_exec($ch);
echo $response;
```

tab: Java
```java
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
    .uri(URI.create("https://your-hatch-server/api/sso/exchange"))
    .header("Content-Type", "application/json")
    .method("POST", HttpRequest.BodyPublishers.ofString("{\"code\":\"string\",\"verifier\":\"string\"}"))
    .build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
```
````

## Example response

````tabs
tab: 200
```json
{
  "user": {
    "id": "string",
    "email": "string",
    "name": "string",
    "image": "string",
    "twoStep": true
  }
}
```

tab: 4XX
```json
{
  "message": "string",
  "code": "string",
  "retryable": true,
  "requestId": "string"
}
```
````
