> For the complete documentation index, see [llms.txt](https://docs.somnia.network/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.somnia.network/developer/building-dapps/data-indexing-and-querying/ormi-subgraph.md).

# Ormi Subgraph

The Graph is a decentralized indexing protocol that allows developers to query blockchain data using GraphQL. Instead of parsing complex raw logs and events directly from smart contracts, developers can build subgraphs that transform onchain activity into structured, queryable datasets.

This tutorial demonstrates how to deploy a subgraph on the Somnia Testnet using [Ormi](https://ormilabs.com), a powerful gateway that simplifies subgraph deployment through a hosted Graph Node and IPFS infrastructure.

{% embed url="<https://www.youtube.com/watch?v=yYKbvJAEX80>" %}

## Prerequisites

* GraphQL is installed and set up on your local machine.
* A verified smart contract address deployed on Somnia.
* An Ormi account and Private Key.

## Install Graph CLI globally

```bash
npm install -g @graphprotocol/graph-cli
```

## Initialize Your Subgraph

The Graph services rely on the existence of a deployed Smart Contract with onchain activity. The subgraph will be created based on indexing the events emitted from the Smart Contract. To set up a subgraph service for an example contract called **`MyToken`** run the following command to scaffold a new subgraph project:

```bash
graph init --contract-name MyToken --from-contract 0xYourTokenAddress --network somnia-testnet mytoken
```

**`mytoken`** is the folder that contains the subgraph files. Replace `0xYourTokenAddress` with your actual deployed Smart Contract address on Somnia.

This command will generate the following files:

* `subgraph.yaml`Defines the data sources and events to index
* `schema.graphql` Structure of your data
* `src/mytoken.ts`TypeScript logic to handle events

## Define the Subgraph Schema <a href="#define-the-subgraph-schema" id="define-the-subgraph-schema"></a>

For the example`MyToken` Contract, which is an ERC20 Token, Edit `schema.graphql` to index all the Transfer events emitted from the Smart Contract.

```typescript
type Transfer @entity(immutable: true) {
  id: Bytes!
  from: Bytes!
  to: Bytes!
  value: BigInt!
  blockNumber: BigInt!
  blockTimestamp: BigInt!
  transactionHash: Bytes!
}
```

## Build the Subgraph

After customizing your schema and mapping logic, build the subgraph by running the command:

```
graph codegen && graph build
```

This will generate the necessary `artifacts` for deployment.

## Deploy Using Ormi

Open the Somnia Ormi website <https://subgraph.somnia.network/> and create an account.

<figure><img src="https://2122549367-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkYErT9t3BJtpPfejLO6I%2Fuploads%2FAm7Rs07ZpEQcTVVmaI4q%2FOrmi-Login.png?alt=media&amp;token=7e677985-72ea-446a-8697-8dc96ac2371c" alt=""><figcaption></figcaption></figure>

On the left navigation menu, click the "key" icon to access your `privateKey.`

<figure><img src="https://2122549367-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkYErT9t3BJtpPfejLO6I%2Fuploads%2FOxEJLWk2vP58mYRizSZh%2FOrmi-pKey.png?alt=media&amp;token=2cf1a5d2-fbe7-4c0a-b27c-89d8950c909d" alt=""><figcaption></figcaption></figure>

Deploy your subgraph to Ormi’s hosted infrastructure with the following command:

```bash
graph deploy mytoken --node https://api.subgraph.somnia.network/deploy --ipfs https://api.subgraph.somnia.network/ipfs --deploy-key yourORMIPrivateKey
```

Replace yourPrivateKey with your Somnia Ormi account private key.

Once deployed, Ormi will return a GraphQL endpoint where you can begin querying your subgraph.

Return to the dashboard to find your list of deployed subgraphs.

<figure><img src="https://2122549367-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkYErT9t3BJtpPfejLO6I%2Fuploads%2FIaqgFZvz9xebeCboNoPL%2FAds%C4%B1z%20tasar%C4%B1m%20(24).png?alt=media&amp;token=3823da8f-a314-4d17-b836-41d410597ec1" alt=""><figcaption></figcaption></figure>

Open the deployed subgraph in the explorer to interact with it:

<figure><img src="https://2122549367-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkYErT9t3BJtpPfejLO6I%2Fuploads%2FMzzQXDCRdUcvrpiLQkW4%2Formi-explorer.png?alt=media&amp;token=8b9f0dcf-7424-49c1-a3d1-510d6a89e324" alt=""><figcaption></figcaption></figure>

## Conclusion

You have successfully deployed a subgraph to index events emitted from your Smart Contract. To challenge yourself even further, you can extend your build:

* Expand your schema and mapping logic to cover more events.
* Connect your subgraph to a frontend UI or analytics dashboard.<br>

For more information, visit the Ormi [docs](https://docs.ormilabs.com/dedicated-env/somnia/subgraphs/overview).


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.somnia.network/developer/building-dapps/data-indexing-and-querying/ormi-subgraph.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
