# Service Tokens

Service tokens are unique identifiers what provides type-safe access to a value stored in a `Container`.

```ts
import 'reflect-metadata';
import { Container, Token } from 'typedi';

export const JWT_SECRET_TOKEN = new Token<string>('MY_SECRET');

Container.set(JWT_SECRET_TOKEN, 'wow-such-secure-much-encryption');

/**
 * Somewhere else in the application after the JWT_SECRET_TOKEN is
 * imported in can be used to request the secret from the Container.
 *
 * This value is type-safe also because the Token is typed.
 */
const JWT_SECRET = Container.get(JWT_SECRET_TOKEN);
```

## Injecting service tokens

They can be used with the `@Inject()` decorator to overwrite the inferred type of the property or argument.

```ts
import 'reflect-metadata';
import { Container, Token, Inject, Service } from 'typedi';

export const JWT_SECRET_TOKEN = new Token<string>('MY_SECRET');

Container.set(JWT_SECRET_TOKEN, 'wow-such-secure-much-encryption');

@Service()
class Example {
  @Inject(JWT_SECRET_TOKEN)
  myProp: string;
}

const instance = Container.get(Example);
// The instance.myProp property has the value assigned for the Token
```

## Tokens with same name

Two token **with the same name are different tokens**. The name is only used to help the developer identify the tokens during debugging and development. (It's included in error the messages.)

```ts
import 'reflect-metadata';
import { Container, Token } from 'typedi';

const tokenA = new Token('TOKEN');
const tokenB = new Token('TOKEN');

Container.set(tokenA, 'value-A');
Container.set(tokenB, 'value-B');

const tokenValueA = Container.get(tokenA);
// tokenValueA is "value-A"
const tokenValueB = Container.get(tokenB);
// tokenValueB is "value-B"

console.log(tokenValueA === tokenValueB);
// returns false, as Tokens are always unique
```

## Difference between Token and string identifier

They both achieve the same goal, however, it's recommended to use `Tokens` as they are type-safe and cannot be mistyped, while a mistyped string identifier will silently return `undefined` as value by default.


---

# Agent Instructions: 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://docs.typestack.community/typedi/02-basic-usage-guide/06-service-tokens.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.
