Copy as Markdown[View as Markdown](/docs/sdk/functions/addSchemaToEvent.md)

***

# Function: addSchemaToEvent()

> **addSchemaToEvent**(`directory`): (`id`, `schema`, `version`?, `options`?) => `Promise`<`void`>

Defined in: events.ts:295

Add a schema to an event by it's id.

Optionally specify a version to add a schema to a specific version of the event.

## Parameters[​](#parameters "Direct link to Parameters")

| Parameter   | Type     |
| ----------- | -------- |
| `directory` | `string` |

## Returns[​](#returns "Direct link to Returns")

`Function`

### Parameters[​](#parameters-1 "Direct link to Parameters")

| Parameter         | Type                                          |
| ----------------- | --------------------------------------------- |
| `id`              | `string`                                      |
| `schema`          | { `fileName`: `string`; `schema`: `string`; } |
| `schema.fileName` | `string`                                      |
| `schema.schema`?  | `string`                                      |
| `version`?        | `string`                                      |
| `options`?        | { `path`: `string`; }                         |
| `options.path`?   | `string`                                      |

### Returns[​](#returns-1 "Direct link to Returns")

`Promise`<`void`>

## Example[​](#example "Direct link to Example")

```
import utils from '@eventcatalog/utils';

const { addFileToEvent } = utils('/path/to/eventcatalog');

// JSON schema example
const schema = {
   "$schema": "http://json-schema.org/draft-07/schema#",
   "type": "object",
   "properties": {
       "name": {
       "type": "string"
   },
   "age": {
     "type": "number"
   }
 },
 "required": ["name", "age"]
};

// adds a file to the latest InventoryAdjusted event
await addFileToEvent('InventoryAdjusted', { schema, fileName: 'schema.json' });

// adds a file to a specific version of the InventoryAdjusted event
await addFileToEvent('InventoryAdjusted', { schema, fileName: 'schema.json' }, '0.0.1');
```
