# Compose a message

> **POST** `https://api.us.nylas.com/v3/grants/{grant_id}/messages/smart-compose`

Source: https://developer.nylas.com/docs/reference/api/smart-compose/post-smart-compose/

Generates a message based on a prompt.

**Authentication:** NYLAS_API_KEY, ACCESS_TOKEN

## Parameters

### Path parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `grant_id` | string | Yes | ID of the grant to access. Use `/me/` to refer to the grant associated with an access token. |

## Request body

Content-Type: application/json

- `prompt` (string) - The prompt that Smart Compose uses to generate a message suggestion.

## Responses

### 200 - Returns a suggestion for a message

- `request_id` (string) - The request ID.
- `data` (object)
  - `suggestion` (string) **(required)**

- `suggestion` (string)

### 400 - Bad Request

- `request_id` (string) - ID of the request
- `error` (object) - Response error object.
  - `type` (string) - Error Type
  - `message` (string) - Error Message

### 401 - Unauthorized

- `request_id` (string) - The request ID.
- `error` (object) - The response error object.
  - `type` (string) - The error type.
  - `message` (string) - The error message.
  - `provider_error` (object) - The error from the provider.

### 422 - Validation Error

- `request_id` (string) - ID of the request
- `error` (object) - Response error object.
  - `type` (string) - Error Type
  - `message` (string) - Error Message
  - `details` (array)
    - `loc` (array)
    - `msg` (string)
    - `type` (string)

### 500 - Unexpected Error

- `request_id` (string) - ID of the request
- `error` (object) - Response error object.
  - `type` (string) - Error Type
  - `message` (string) - Error Message

## Code samples

### cURL

```bash
curl --request POST \
  --url 'https://api.us.nylas.com/v3/grants/<NYLAS_GRANT_ID>/messages/smart-compose' \
  --header 'Accept: application/json' \
  --header 'Authorization: Bearer <NYLAS_API_KEY>' \
  --header 'Content-Type: application/json'
```

### Node.js SDK

```javascript
import Nylas from "nylas";

const nylas = new Nylas({
  apiKey: "<NYLAS_API_KEY>",
  apiUri: "<NYLAS_API_URI>",
});

async function composeEmail() {
  try {
    const message = await nylas.messages.smartCompose.composeMessage({
      identifier: "<NYLAS_GRANT_ID>",
      requestBody: {
        prompt: "Tell my colleague how we can use Nylas APIs",
      },
    });

    console.log("Message created:", message);
  } catch (error) {
    console.error("Error creating message:", error);
  }
}

composeEmail();

```

### Python SDK

```python
from nylas import Client

nylas = Client(
    "<NYLAS_API_KEY>",
    "<NYLAS_API_URI>"
)

grant_id = "<NYLAS_GRANT_ID>"
email = "<EMAIL>"

message = nylas.messages.smart_compose.compose_message(
  grant_id,
  request_body={
    "prompt": "Tell my colleague how we can use Nylas APIs",
  }
)

print(message)
```

### Ruby SDK

```ruby
require 'nylas'

nylas = Nylas::Client.new(
  api_key: "<NYLAS_API_KEY>"
)

request_body = {
  prompt: "Let's talk about Nylas"
}

message, _ = nylas.messages.smart_compose.compose_message(
  identifier: "<NYLAS_GRANT_ID>",
  request_body: request_body
)

puts message[:suggestion]

```

### Java SDK

```java
import com.nylas.NylasClient;
import com.nylas.models.*;

public class SmartCompose {
  public static void main(String[] args) throws NylasSdkTimeoutError, NylasApiError {
    NylasClient nylas = new NylasClient.Builder("<NYLAS_API_KEY>").build();
    ComposeMessageRequest requestBody = new ComposeMessageRequest("Let's talk about Nylas");
    Response<ComposeMessageResponse> message = nylas.messages().smartCompose().composeMessage("<NYLAS_GRANT_ID>", requestBody);
    
    System.out.println(message.getData().getSuggestion());
  }
}

```

### Kotlin SDK

```kotlin
import com.nylas.NylasClient
import com.nylas.models.ComposeMessageRequest

fun main(args: Array<String>) {
  val nylas: NylasClient = NylasClient(
      apiKey = "<NYLAS_API_KEY>"
  )

  val requestBody : ComposeMessageRequest = ComposeMessageRequest("Let's talk about Nylas")
  val message = nylas.messages().smartCompose().composeMessage("<NYLAS_GRANT_ID>", requestBody)
  
  print(message.data.suggestion)
}

```
