> For the complete documentation index, see [llms.txt](https://deadeyejoe.gitbook.io/fabrikk/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://deadeyejoe.gitbook.io/fabrikk/tutorial/factories-and-building.md).

# Factories & Building

First we'll learn how to create factories and how to use them to build entities. Let's say our application has users that we want to create for use in our tests, and each user has:

* a name
* an email
* a role - one of 'admin' or 'user'

We might write our factory definition as:

```clojure
(ns fab.tutorial
  (:require [fabrikk.core.alpha :as fab]))

(def user
  (fab/->factory
    ::user
    {:template {:name "John Smith"
                :email "john@example.org"
                :role "admin"}}) 
```

To create a factory we need an `id` and a factory definition; a map with at least a `template` key:

* The id is a keyword that uniquely identifies the factory, namespaced keywords are recommended. Fabrikk accepts factory ids and instances interchangeably in most places (see [API](/fabrikk/reference/api.md#greater-than-factory) for more)
* The template details the keys that our entity will have, and the values of those keys

We'll explore more options in the course of this tutorial, and you can take a look at [API](/fabrikk/reference/api.md#factory-definition) to see a comprehensive list.

Now that we have a  factory we can get building:

```clojure
(fab/build user) 
;; => {:name "John Smith", :email "john@example.org", :role "admin"}
(fab/build user) 
;; => {:name "John Smith", :email "john@example.org", :role "admin"}
```

But wait, this generates the same user every time! We can pass *build options* to `build` to vary the output, using the `with` option:

```clojure
;; we use 'with' to change the name:
(fab/build user {:with {:name "John Murphy"}})
;; => {:name "John Murphy", :email "john@example.org", :role "admin"}

;; or add an arbitrary key to the entity:
(fab/build user {:with {:favourite-food "chips"}})
;; => {:name "John Smith", :email "john@example.org", :role "admin", 
;;     :favourite-food "chips"}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://deadeyejoe.gitbook.io/fabrikk/tutorial/factories-and-building.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
