---
title: Connecting repositories to services
source: https://docs.newrelic.com/docs/codestream/observability/repo-association
---

The telemetry data available in CodeStream is contextual, meaning that it's all related to the code you have open in your IDE. To do this, CodeStream needs to know what services in New Relic are built from the repositories you have open in your IDE. CodeStream will prompt you to select a service to associate with the repository you currently have open in your IDE.

![A screenshot of associating a repository in your IDE with a service.](https://docs.newrelic.com/images/codestream_screenshot-crop_associate-repos-button.webp "Associate a repository in your IDE with a service.")

In some cases your repository may need to be associated with multiple services. For example, you might have different services that represent different environments (like production, or staging) and they may all be associated with the same repository. To associate the current repository with an additional service, click on **Add another service**.

![A screenshot of associating an additional service with your repository.](https://docs.newrelic.com/images/codestream_screenshot-crop_associate-other-services.webp "Associate an additional service with your repository.")

Making these associations on the fly when prompted is a great way to get started, but we recommend one of the following methods because they require less ongoing manual effort and eliminate the possibility of end-user mistakes, such as misconfigured remote URLs.

**Use environment variable with APM (recommended)**

Set the environment variable `NEW_RELIC_METADATA_REPOSITORY_URL`. New Relic APM agents create the repository entity and associate it with your application entity automatically.

This requires the SSH or HTTPS remote URL format. We recommend that this be set as part of your build pipeline.

**Use the New Relic web UI**

Go to the APM summary page via **[one.newrelic.com > All capabilities](https://one.newrelic.com/all-capabilities) > APM & Services > (select an app)**, then look click on the ellipses menu to the right of the service name. Click on the **Repositories** tab to connect a repository.

![A screenshot of the Repositories section of the service summary page.](https://docs.newrelic.com/images/codestream_screenshot-crop_connect-repositories.webp "The Repositories section of the service summary page")

Click **Connect repository** to find an existing repository or add a new one.

**Use the NerdGraph API**

Use New Relic's [NerdGraph APIs](https://docs.newrelic.com/docs/apis/nerdgraph/get-started/introduction-new-relic-nerdgraph/) to create a repository and associate it with your application entities.

**Step 1: Create a repository entity**

To create a repository entity, use the `referenceEntityCreateOrUpdateRepository` API and make sure to save the GUID that's produced. The API takes the following parameters:

-   `accountId` - the integer account ID for the account you want to add the repository to

-   `url` - example `https://github.com/newrelic/beta-docs-site.git`

-   `name` - example: `newrelic/beta-docs-site`

    ```graphql
    mutation {
      referenceEntityCreateOrUpdateRepository(repositories: [{accountId: [YOUR_ACCOUNT_ID], name: "[REPO_NAME]", url: "[REPO_URL]"}]) {
        created
        failures {
          guid
          message
          type
        }
      }
    }
    ```

    To find the entity you create, you can use a query like the following. Note that the URL you provided to `referenceEntityCreateOrUpdateRepository` gets saved as an entity tag.

    ```graphql
    {
      actor {
        entitySearch(query: "name = 'a name' OR tags.url = 'a url'") {
          count
          query
          results {
            entities {
              guid
              name
              tags {
                key
                values
              }
            }
          }
        }
      }
    }

    ```

    **Step 2: Associate the repository entity to your application entity**

    First, find the GUID for the application you want to associate your repository to.

    Parameters:

-   `sourceEntityGuid` - the entity GUID of the application

-   `targetEntityGuid` - the entity GUID of your repository

-   `type` - always `BUILT_FROM`

    ```graphql
    mutation {
      entityRelationshipUserDefinedCreateOrReplace(sourceEntityGuid: "", targetEntityGuid: "", type: BUILT_FROM) {
        errors {
          message
          type
        }
      }
    }
    ```

    To see all entities related to your repository you can do a query like this:

    ```graphql
    {
      actor {
        entity(guid: "[YOUR_REPOSITORY_GUID]]") {
          relatedEntities(filter: {direction: BOTH, relationshipTypes: {include: BUILT_FROM}}) {
            results {
              target {
                entity {
                  name
                  guid
                  type
                }
              }
              type
            }
          }
          name
          type
          tags {
            values
            key
          }
        }
      }
    }
    ```

    **Step 3: Cleanup (if needed)**

    Delete a repository with the following query:

    ```graphql
    mutation DeleteRepository {
      entityDelete(guids: "[ENTITY_GUID_HERE]]") {
        deletedEntities
        failures {
          message
          guid
        }
      }
    }
    ```

With any of these methods you can specify the remote URL in either the SSH or HTTPS format:

-   `git@github.com:newrelic/[GITHUB_REPO_NAME_HERE].git`
-   `https://github.com/newrelic/[GITHUB_REPO_NAME_HERE].git`

> #### ⚠️ CAUTION
>
> It's possible to add the same repository more than once, if you're using different protocols to do so. The UI warns you about this, but won't prevent you from doing so.
>
> For example, `https://github.com/tuna/repo` and `git@github.com:tuna/repo` are the same repo, with different protocols.
