# API Reference
**Classes**
Name|Description
----|-----------
[Construct](#constructs-construct)|Represents the building block of the construct graph.
[ConstructMetadata](#constructs-constructmetadata)|Metadata keys used by constructs.
[Node](#constructs-node)|Represents the construct node in the scope tree.
**Structs**
Name|Description
----|-----------
[ConstructOptions](#constructs-constructoptions)|Options for creating constructs.
[Dependency](#constructs-dependency)|A single dependency.
[MetadataEntry](#constructs-metadataentry)|An entry in the construct metadata table.
[SynthesisOptions](#constructs-synthesisoptions)|Options for synthesis.
[ValidationError](#constructs-validationerror)|An error returned during the validation phase.
**Interfaces**
Name|Description
----|-----------
[IAspect](#constructs-iaspect)|Represents an Aspect.
[IConstruct](#constructs-iconstruct)|Represents a construct.
[INodeFactory](#constructs-inodefactory)|A factory for attaching `Node`s to the construct.
[ISynthesisSession](#constructs-isynthesissession)|Represents a single session of synthesis.
[IValidation](#constructs-ivalidation)|Implement this interface in order for the construct to be able to validate itself.
**Enums**
Name|Description
----|-----------
[ConstructOrder](#constructs-constructorder)|In what order to return constructs.
## class Construct
Represents the building block of the construct graph.
All constructs besides the root construct must be created within the scope of
another construct.
__Implements__: [IConstruct](#constructs-iconstruct)
### Initializer
Creates a new construct node.
```ts
new Construct(scope: Construct, id: string, options?: ConstructOptions)
```
* **scope** ([Construct](#constructs-construct)
) The scope in which to define this construct.
* **id** (string
) The scoped construct ID.
* **options** ([ConstructOptions](#constructs-constructoptions)
) Options.
* **nodeFactory** ([INodeFactory](#constructs-inodefactory)
) A factory for attaching `Node`s to the construct. __*Default*__: the default `Node` is associated
### Methods
#### toString()
Returns a string representation of this construct.
```ts
toString(): string
```
__Returns__:
* string
#### protected onPrepare()
Perform final modifications before synthesis.
This method can be implemented by derived constructs in order to perform
final changes before synthesis. prepare() will be called after child
constructs have been prepared.
This is an advanced framework feature. Only use this if you
understand the implications.
```ts
protected onPrepare(): void
```
#### protected onSynthesize(session)
Allows this construct to emit artifacts into the cloud assembly during synthesis.
This method is usually implemented by framework-level constructs such as `Stack` and `Asset`
as they participate in synthesizing the cloud assembly.
```ts
protected onSynthesize(session: ISynthesisSession): void
```
* **session** ([ISynthesisSession](#constructs-isynthesissession)
) The synthesis session.
#### protected onValidate()⚠️
Validate the current construct.
This method can be implemented by derived constructs in order to perform
validation logic. It is called on all constructs before synthesis.
```ts
protected onValidate(): Array
```
__Returns__:
* Array
## class ConstructMetadata
Metadata keys used by constructs.
### Properties
Name | Type | Description
-----|------|-------------
*static* **DISABLE_STACK_TRACE_IN_METADATA** | string
| If set in the construct's context, omits stack traces from metadata entries.
*static* **ERROR_METADATA_KEY** | string
| Context type for error level messages.
*static* **INFO_METADATA_KEY** | string
| Context type for info level messages.
*static* **WARNING_METADATA_KEY** | string
| Context type for warning level messages.
## class Node
Represents the construct node in the scope tree.
### Initializer
```ts
new Node(host: Construct, scope: IConstruct, id: string)
```
* **host** ([Construct](#constructs-construct)
) *No description*
* **scope** ([IConstruct](#constructs-iconstruct)
) *No description*
* **id** (string
) *No description*
### Properties
Name | Type | Description
-----|------|-------------
**addr** | string
| Returns an opaque tree-unique address for this construct.
**children** | Array<[IConstruct](#constructs-iconstruct)>
| All direct children of this construct.
**dependencies** | Array<[Dependency](#constructs-dependency)>
| Return all dependencies registered on this node or any of its children.
**id** | string
| The id of this construct within the current scope.
**locked** | boolean
| Returns true if this construct or the scopes in which it is defined are locked.
**metadata** | Array<[MetadataEntry](#constructs-metadataentry)>
| An immutable array of metadata objects associated with this construct.
**path** | string
| The full, absolute path of this construct in the tree.
**root** | [IConstruct](#constructs-iconstruct)
| Returns the root of the construct tree.
**scopes** | Array<[IConstruct](#constructs-iconstruct)>
| All parent scopes of this construct.
**uniqueId**⚠️ | string
| A tree-global unique alphanumeric identifier for this construct.
**defaultChild**? | [IConstruct](#constructs-iconstruct)
| Returns the child construct that has the id `Default` or `Resource"`.
__*Optional*__
**scope**? | [IConstruct](#constructs-iconstruct)
| Returns the scope in which this construct is defined.
__*Optional*__
*static* **PATH_SEP** | string
| Separator used to delimit construct path components.
### Methods
#### addDependency(...dependencies)
Add an ordering dependency on another Construct.
All constructs in the dependency's scope will be deployed before any
construct in this construct's scope.
```ts
addDependency(...dependencies: IConstruct[]): void
```
* **dependencies** ([IConstruct](#constructs-iconstruct)
) *No description*
#### addError(message)
Adds an { "error": } metadata entry to this construct.
The toolkit will fail synthesis when errors are reported.
```ts
addError(message: string): void
```
* **message** (string
) The error message.
#### addInfo(message)
Adds a { "info": } metadata entry to this construct.
The toolkit will display the info message when apps are synthesized.
```ts
addInfo(message: string): void
```
* **message** (string
) The info message.
#### addMetadata(type, data, fromFunction?)
Adds a metadata entry to this construct.
Entries are arbitrary values and will also include a stack trace to allow tracing back to
the code location for when the entry was added. It can be used, for example, to include source
mapping in CloudFormation templates to improve diagnostics.
```ts
addMetadata(type: string, data: any, fromFunction?: any): void
```
* **type** (string
) a string denoting the type of metadata.
* **data** (any
) the value of the metadata (can be a Token).
* **fromFunction** (any
) a function under which to restrict the metadata entry's stack trace (defaults to this.addMetadata).
#### addValidation(validation)
Adds a validation to this construct.
When `node.validate()` is called, the `validate()` method will be called on
all validations and all errors will be returned.
```ts
addValidation(validation: IValidation): void
```
* **validation** ([IValidation](#constructs-ivalidation)
) *No description*
#### addWarning(message)
Adds a { "warning": } metadata entry to this construct.
The toolkit will display the warning when an app is synthesized, or fail
if run in --strict mode.
```ts
addWarning(message: string): void
```
* **message** (string
) The warning message.
#### applyAspect(aspect)
Applies the aspect to this Constructs node.
```ts
applyAspect(aspect: IAspect): void
```
* **aspect** ([IAspect](#constructs-iaspect)
) *No description*
#### findAll(order?)
Return this construct and all of its children in the given order.
```ts
findAll(order?: ConstructOrder): Array
```
* **order** ([ConstructOrder](#constructs-constructorder)
) *No description*
__Returns__:
* Array<[IConstruct](#constructs-iconstruct)>
#### findChild(id)
Return a direct child by id.
Throws an error if the child is not found.
```ts
findChild(id: string): IConstruct
```
* **id** (string
) Identifier of direct child.
__Returns__:
* [IConstruct](#constructs-iconstruct)
#### prepare()
Invokes "prepare" on all constructs (depth-first, post-order) in the tree under `node`.
```ts
prepare(): void
```
#### setContext(key, value)
This can be used to set contextual values.
Context must be set before any children are added, since children may consult context info during construction.
If the key already exists, it will be overridden.
```ts
setContext(key: string, value: any): void
```
* **key** (string
) The context key.
* **value** (any
) The context value.
#### synthesize(options)
Synthesizes a CloudAssembly from a construct tree.
```ts
synthesize(options: SynthesisOptions): void
```
* **options** ([SynthesisOptions](#constructs-synthesisoptions)
) Synthesis options.
* **outdir** (string
) The output directory into which to synthesize the cloud assembly.
* **sessionContext** (Map
) Additional context passed into the synthesis session object when `construct.synth` is called. __*Default*__: no additional context is passed to `onSynthesize`
* **skipValidation** (boolean
) Whether synthesis should skip the validation phase. __*Default*__: false
#### tryFindChild(id)
Return a direct child by id, or undefined.
```ts
tryFindChild(id: string): IConstruct
```
* **id** (string
) Identifier of direct child.
__Returns__:
* [IConstruct](#constructs-iconstruct)
#### tryGetContext(key)
Retrieves a value from tree context.
Context is usually initialized at the root, but can be overridden at any point in the tree.
```ts
tryGetContext(key: string): any
```
* **key** (string
) The context key.
__Returns__:
* any
#### tryRemoveChild(childName)🔹
Remove the child with the given name, if present.
```ts
tryRemoveChild(childName: string): boolean
```
* **childName** (string
) *No description*
__Returns__:
* boolean
#### validate()
Validates tree (depth-first, pre-order) and returns the list of all errors.
An empty list indicates that there are no errors.
```ts
validate(): Array
```
__Returns__:
* Array<[ValidationError](#constructs-validationerror)>
#### *static* of(construct)
Returns the node associated with a construct.
```ts
static of(construct: IConstruct): Node
```
* **construct** ([IConstruct](#constructs-iconstruct)
) the construct.
__Returns__:
* [Node](#constructs-node)
## struct ConstructOptions
Options for creating constructs.
Name | Type | Description
-----|------|-------------
**nodeFactory**? | [INodeFactory](#constructs-inodefactory)
| A factory for attaching `Node`s to the construct.
__*Default*__: the default `Node` is associated
## struct Dependency
A single dependency.
Name | Type | Description
-----|------|-------------
**source** | [IConstruct](#constructs-iconstruct)
| Source the dependency.
**target** | [IConstruct](#constructs-iconstruct)
| Target of the dependency.
## interface IAspect
Represents an Aspect.
### Methods
#### visit(node)
All aspects can visit an IConstruct.
```ts
visit(node: IConstruct): void
```
* **node** ([IConstruct](#constructs-iconstruct)
) *No description*
## interface IConstruct
__Implemented by__: [Construct](#constructs-construct)
__Obtainable from__: [Node](#constructs-node).[findChild](#constructs-node#constructs-node-findchild)(), [Node](#constructs-node).[tryFindChild](#constructs-node#constructs-node-tryfindchild)()
Represents a construct.
## interface INodeFactory
A factory for attaching `Node`s to the construct.
### Methods
#### createNode(host, scope, id)
Returns a new `Node` associated with `host`.
```ts
createNode(host: Construct, scope: IConstruct, id: string): Node
```
* **host** ([Construct](#constructs-construct)
) the associated construct.
* **scope** ([IConstruct](#constructs-iconstruct)
) the construct's scope (parent).
* **id** (string
) the construct id.
__Returns__:
* [Node](#constructs-node)
## interface ISynthesisSession
Represents a single session of synthesis.
Passed into `construct.onSynthesize()` methods.
### Properties
Name | Type | Description
-----|------|-------------
**outdir** | string
| The output directory for this synthesis session.
## interface IValidation
Implement this interface in order for the construct to be able to validate itself.
### Methods
#### validate()
Validate the current construct.
This method can be implemented by derived constructs in order to perform
validation logic. It is called on all constructs before synthesis.
```ts
validate(): Array
```
__Returns__:
* Array
## struct MetadataEntry
An entry in the construct metadata table.
Name | Type | Description
-----|------|-------------
**data** | any
| The data.
**type** | string
| The metadata entry type.
**trace**? | Array
| Stack trace.
__*Default*__: no trace information
## struct SynthesisOptions
Options for synthesis.
Name | Type | Description
-----|------|-------------
**outdir** | string
| The output directory into which to synthesize the cloud assembly.
**sessionContext**? | Map
| Additional context passed into the synthesis session object when `construct.synth` is called.
__*Default*__: no additional context is passed to `onSynthesize`
**skipValidation**? | boolean
| Whether synthesis should skip the validation phase.
__*Default*__: false
## struct ValidationError
An error returned during the validation phase.
Name | Type | Description
-----|------|-------------
**message** | string
| The error message.
**source** | [Construct](#constructs-construct)
| The construct which emitted the error.
## enum ConstructOrder
In what order to return constructs.
Name | Description
-----|-----
**PREORDER** |Depth-first, pre-order.
**POSTORDER** |Depth-first, post-order (leaf nodes first).