A


[](https://fuchsia.dev/docs/glossary#service-capability)


is a capability that
enables discovery of one or more individually named


[](https://fuchsia.dev/docs/glossary#service)


instances. Service capabilities are backed by
a


[](https://fuchsia.dev/docs/glossary#channel)


that speaks the [`Directory`](https://fuchsia.dev/reference/fidl/fuchsia.io#Directory) protocol,
where each entry in the directory exposes a named [service instance](https://fuchsia.dev/fuchsia-src/concepts/components/v2/capabilities/service#instances).

    library fuchsia.examples;

    const MAX_STRING_LENGTH uint64 = 32;

    @discoverable
    protocol Echo {
        EchoString(struct {
            value string:MAX_STRING_LENGTH;
        }) -> (struct {
            response string:MAX_STRING_LENGTH;
        });
        SendString(struct {
            value string:MAX_STRING_LENGTH;
        });
        -> OnString(struct {
            response string:MAX_STRING_LENGTH;
        });
    };

    service EchoService {
        regular_echo client_end:Echo;
        reversed_echo client_end:Echo;
    };

> [!NOTE]
> **Note:** For more details on FIDL service syntax, see the [FIDL language reference](https://fuchsia.dev/docs/reference/fidl/language/language).

Service implementations are served from provider components using the


[](https://fuchsia.dev/docs/glossary#outgoing-directory)


and consumed from another
component's


[](https://fuchsia.dev/docs/glossary#namespace)


.

## Service instances

Multiple named instances of a service can be hosted by a single component.
These are present in the


[](https://fuchsia.dev/docs/glossary#namespace)


of the consuming
component as subdirectories of the service.
The component framework generates an arbitrary, unique identifier for each
service instance name.

For example, if the framework generates `57dfe118a2a8` as the instance name of
the `fuchsia.examples.EchoService` service, a consuming component could connect
to the protocols in that instance using the following namespace paths:

- `/svc/fuchsia.examples.EchoService/57dfe118a2a8/regular_echo`
- `/svc/fuchsia.examples.EchoService/57dfe118a2a8/reversed_echo`

## Providing service capabilities

To provide a service capability, a component must declare the capability and
[route](https://fuchsia.dev/fuchsia-src/concepts/components/v2/capabilities/service#route) it from `self`. The component hosts the service capability in
its


[](https://fuchsia.dev/docs/glossary#outgoing-directory)


.

To define the capability, add a `capabilities` declaration for it:

    {
        capabilities: [
            {
                service: "fuchsia.example.ExampleService",
            },
        ],
    }

This defines a capability hosted by this component whose outgoing directory path
is `/svc/fuchsia.example.ExampleService`. You can also customize the path:

    {
        capabilities: [
            {
                service: "fuchsia.example.ExampleService",
                path: "/my_svc/fuchsia.example.MyExampleService",
            },
        ],
    }

## Routing service capabilities

Components route service capabilities by [exposing](https://fuchsia.dev/fuchsia-src/concepts/components/v2/capabilities/service#expose) them to their
parent and [offering](https://fuchsia.dev/fuchsia-src/concepts/components/v2/capabilities/service#offer) them to their children.

For more details on how the framework routes component capabilities,
see [capability routing](https://fuchsia.dev/docs/concepts/components/v2/capabilities/README#routing).

### Exposing

Exposing a service capability gives the component's parent access to that
capability:

    {
        expose: [
            {
                service: "fuchsia.example.ExampleService",
                from: "self",
            },
        ],
    }

The `from: "self"` directive means that the service capability is
[provided](https://fuchsia.dev/fuchsia-src/concepts/components/v2/capabilities/service#provide) by this component.

#### Dynamic collections

A service capability can be exposed from a [dynamic collection](https://fuchsia.dev/docs/concepts/components/v2/realms#collections):

    {
        collections: [
            {
                name: "coll",
                durability: "transient",
            },
        ],
        expose: [
            {
                service: "fuchsia.example.ExampleService",
                from: "#coll",
            },
        ],
    }

> [!NOTE]
> **Note:** When routing services exposed from components in the collection, the component framework renames each [service instance](https://fuchsia.dev/fuchsia-src/concepts/components/v2/capabilities/service#instances) with an arbitrary, unique identifier to allow multiple components in the collection to expose the same service.

### Offering

Offering a service capability gives a child component access to that
capability:

    {
        offer: [
            {
                service: "fuchsia.example.ExampleService",
                from: "self",
                to: [ "#child-a", "#child_b" ],
            },
        ],
    }

## Consuming service capabilities

To consume a service capability, the component must request the capability and
open the corresponding path in its


[](https://fuchsia.dev/docs/glossary#namespace)


.

To request the capability, add a `use` declaration for it:

    {
        use: [
            {
                service: "fuchsia.example.ExampleService",
            },
        ],
    }

This populates the service in the component's namespace at the well-known path
`/svc/fuchsia.example.ExampleService`. You can also customize the path:

    {
        use: [
            {
                service: "fuchsia.example.ExampleService",
                path: "/my_svc/fuchsia.example.MyExampleService",
            },
        ],
    }

For more information about the open request, see
[life of a protocol open](https://fuchsia.dev/docs/concepts/components/v2/capabilities/life_of_a_protocol_open).

> [!NOTE]
> **Note:** For a working example of routing a service capability between components, see [`//examples/components/services`](https://cs.opensource.google/fuchsia/fuchsia/+/main:/examples/components/services).

[](https://issues.fuchsia.dev/issues/new?component=1439230&template=1881640&description=Issue+on:+https://fuchsia.dev/docs/concepts/components/v2/capabilities/service.md "Report a bug or suggest improvements on this page") [](https://cs.opensource.google/fuchsia/fuchsia/+/main:docs/concepts/components/v2/capabilities/service.md "View the source code of this page") [](https://ci.android.com/edit?repo=fuchsia/fuchsia/main&file=docs/concepts/components/v2/capabilities/service.md&cloneRepo=fuchsia/fuchsia:refs/heads/main&fullPath=fuchsia/docs/concepts/components/v2/capabilities/service.md "Edit the source code of this page")