Service Module

This document (version 0.1) defines a service module according to the terms set out in the core specification.

The service module enables blocks to interact with external services without needing to be provided with credentials: the embedding application calls the external service on the block's behalf.

Glossary

Provider: a company, organization or person that provides a service or services (e.g. Mapbox)

Service: a request handler which meets a specific functional goal, typically external to the embedding application and accessed via third-party API endpoint or integration (e.g. Retrieve Address).

Messages

Handling messages

Summary

Any messages exchanged between application and block should be consistent with the shape and intent of the messages as described in this specification.

While the messages are based on APIs available from external service providers, how exactly the embedding application generates responses to the block is beyond the scope of the Block Protocol, so long as the schema and intent of the message as described in this specification are met – i.e. the embedding application need not call the external service provider if they have another means of generating an appropriate response.

Specification

Embedding applications MUST ensure that any responses they provide to blocks to messages sent under the service module are consistent with the schema and described intent of the message in this specification, and consistent with the response that the named service provider would return, except for any fields which are required by the named service provider but omitted from this specification (e.g. because they relate to authentication or other information that the block should not or does not have access to, and which can be added by the embedding application when making a request to an external service provider in order to produce a response for the block).

Embedding applications MAY generate a response to a block's message by doing any of the following:

  • calling the external service provider directly (augmenting the block's original request with any additional fields or headers the service provider requires), OR
  • generating the response internally, OR
  • calling a third party that provides an appropriate response, whether the third party generates this response directly or by themselves making a request to the named service provider.

Error definitions

Specification

If a request fails, embedding applications MUST provide blocks with an error which complies with this specification, i.e. contains a listed code and a message.

Embedding applications MAY withhold or translate error codes and messages sent by the service provider when reporting an error to the block.

Blocks MUST NOT rely on error codes and messages providing information that the named service provider would if contacted directly, e.g. relating to billing.

Message definitions

Summary

The message signatures in the service specification aim to be as consistent as possible with the equivalent requests and responses from the named service provider, subject to exceptions mentioned elsewhere in this specification.

Specification

The messages available for exchange in the service module are defined in the module's JSON definition and are also listed below for ease of reference.

openaiCreateImage
Create an image using OpenAI - see https://platform.openai.com/docs/api-reference/images
  • source: block
  • data: object
    propertytyperequireddescription
    sizestringnoThe size of the generated images. Must be one of `256x256`, `512x512`, or `1024x1024`.
    promptstringyesA text description of the desired image(s). The maximum length is 1000 characters.
    nnumbernoThe number of images to generate. Must be between 1 and 10.
    response_formatstringnoThe format in which the generated images are returned. Must be one of `url` or `b64_json`.
  • errorCodes: none
  • respondedToBy: openaiCreateImageResponse
  • sentOnInitialization: false
openaiCreateImageResponse
Response to a create OpenAI image request
  • source: embedder
  • data: object
    propertytyperequireddescription
    creatednumberyes
    dataobject[]yes
  • errorCodes: FORBIDDEN, INTERNAL_ERROR, INVALID_INPUT, NOT_IMPLEMENTED, TOO_MANY_REQUESTS, UNAUTHORIZED
  • respondedToBy: none
  • sentOnInitialization: false
openaiCompleteChat
Complete a chat using OpenAI
  • source: block
  • data: object
    propertytyperequireddescription
    stopnoUp to 4 sequences where the API will stop generating further tokens.
    nnumbernoHow many chat completion choices to generate for each input message.
    modelstringyesID of the model to use. Currently, only `gpt-3.5-turbo` and `gpt-3.5-turbo-0301` are supported.
    max_tokensnumbernoThe maximum number of tokens allowed for the generated answer. By default, the number of tokens the model can return will be (4096 - prompt tokens).
    temperaturenumbernoWhat sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. We generally recommend altering this or `top_p` but not both.
    top_pnumbernoAn alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered. We generally recommend altering this or `temperature` but not both.
    streambooleannoIf set, partial message deltas will be sent, like in ChatGPT. Tokens will be sent as data-only [server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format) as they become available, with the stream terminated by a `data: [DONE]` message.
    presence_penaltynumbernoNumber between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model\'s likelihood to talk about new topics. [See more information about frequency and presence penalties.](/docs/api-reference/parameter-details)
    frequency_penaltynumbernoNumber between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model\'s likelihood to repeat the same line verbatim. [See more information about frequency and presence penalties.](/docs/api-reference/parameter-details)
    logit_biasobjectnoModify the likelihood of specified tokens appearing in the completion. Accepts a json object that maps tokens (specified by their token ID in the tokenizer) to an associated bias value from -100 to 100. Mathematically, the bias is added to the logits generated by the model prior to sampling. The exact effect will vary per model, but values between -1 and 1 should decrease or increase likelihood of selection; values like -100 or 100 should result in a ban or exclusive selection of the relevant token.
    messagesobject[]yesThe messages to generate chat completions for, in the [chat format](/docs/guides/chat/introduction).
  • errorCodes: none
  • respondedToBy: openaiCompleteChatResponse
  • sentOnInitialization: false
openaiCompleteChatResponse
Response to a OpenAI complete text request
  • source: embedder
  • data: object
    propertytyperequireddescription
    idstringyes
    objectstringyes
    creatednumberyes
    modelstringyes
    choicesobject[]yes
    usageobjectno
  • errorCodes: none
  • respondedToBy: none
  • sentOnInitialization: false
mapboxForwardGeocoding
Forward geocoding request – see https://docs.mapbox.com/api/search/geocoding
  • source: block
  • data: object
    propertytyperequireddescription
    searchTextstringyes
    optionsArgobjectno
  • errorCodes: none
  • respondedToBy: mapboxForwardGeocodingResponse
  • sentOnInitialization: false
mapboxForwardGeocodingResponse
Response to a forward geocoding request
  • source: embedder
  • data: object
    propertytyperequireddescription
    typestringyes`"FeatureCollection"`, a GeoJSON type from the [GeoJSON specification](https://tools.ietf.org/html/rfc7946).
    querystring[]yesForward geocodes: An array of space and punctuation-separated strings from the original query. Reverse geocodes: An array containing the coordinates being queried.
    features[]yesThe returned feature objects.
    attributionstringyesAttributes the results of the Mapbox Geocoding API to Mapbox.
  • errorCodes: FORBIDDEN, INTERNAL_ERROR, INVALID_INPUT, NOT_IMPLEMENTED, TOO_MANY_REQUESTS, UNAUTHORIZED
  • respondedToBy: none
  • sentOnInitialization: false
mapboxReverseGeocoding
Reverse geocoding request – see https://docs.mapbox.com/api/search/geocoding
  • source: block
  • data: object
    propertytyperequireddescription
    lngLatyes
    optionsArgobjectno
  • errorCodes: none
  • respondedToBy: mapboxReverseGeocodingResponse
  • sentOnInitialization: false
mapboxReverseGeocodingResponse
The response to a request geocoding request
  • source: embedder
  • data: object
    propertytyperequireddescription
    typestringyes`"FeatureCollection"`, a GeoJSON type from the [GeoJSON specification](https://tools.ietf.org/html/rfc7946).
    querystring[]yesForward geocodes: An array of space and punctuation-separated strings from the original query. Reverse geocodes: An array containing the coordinates being queried.
    features[]yesThe returned feature objects.
    attributionstringyesAttributes the results of the Mapbox Geocoding API to Mapbox.
  • errorCodes: FORBIDDEN, INTERNAL_ERROR, INVALID_INPUT, NOT_IMPLEMENTED, TOO_MANY_REQUESTS, UNAUTHORIZED
  • respondedToBy: none
  • sentOnInitialization: false
mapboxRetrieveDirections
Retrieve directions request – see https://docs.mapbox.com/api/navigation/directions
  • source: block
  • data:
  • errorCodes: none
  • respondedToBy: mapboxRetrieveDirectionsResponse
  • sentOnInitialization: false
mapboxRetrieveRetrieveDirectionsResponse
The response to a retrieve directions request
  • source: embedder
  • data: object
    propertytyperequireddescription
    codestringyes
    routes[]yes
  • errorCodes: FORBIDDEN, INTERNAL_ERROR, INVALID_INPUT, NOT_IMPLEMENTED, TOO_MANY_REQUESTS, UNAUTHORIZED
  • respondedToBy: none
  • sentOnInitialization: false
mapboxRetrieveIsochrones
Retrieve isochrones – see https://docs.mapbox.com/api/navigation/isochrone
  • source: block
  • data: object
    propertytyperequireddescription
    profilestringyes
    coordinatesarray[]yes
    contours_minutesnumberyes
    contours_metersnumberyes
    contours_colorsstringno
    polygonsbooleanno
    denoisenumberno
    generalizenumberno
  • errorCodes: none
  • respondedToBy: mapboxRetrieveIsochronesResponse
  • sentOnInitialization: false
mapboxRetrieveIsochronesResponse
The response to a retrieve isochrones request
  • source: embedder
  • data: object
    propertytyperequireddescription
    features[]yes
  • errorCodes: FORBIDDEN, INTERNAL_ERROR, INVALID_INPUT, NOT_IMPLEMENTED, TOO_MANY_REQUESTS, UNAUTHORIZED
  • respondedToBy: none
  • sentOnInitialization: false
mapboxSuggestAddress
Suggest an address – see https://docs.mapbox.com/api/search/search/#retrieve-a-suggestion
  • source: block
  • data: object
    propertytyperequireddescription
    searchTextstringyes
    optionsArgyes
  • errorCodes: none
  • respondedToBy: mapboxSuggestAddressResponse
  • sentOnInitialization: false
mapboxSuggestAddressResponse
The response to a request to suggest an address
  • source: embedder
  • data: object
    propertytyperequireddescription
    attributionstringnoThe attribution data for results.
    suggestionsobject[]yesThe returned suggestion objects.
  • errorCodes: FORBIDDEN, INTERNAL_ERROR, INVALID_INPUT, NOT_IMPLEMENTED, TOO_MANY_REQUESTS, UNAUTHORIZED
  • respondedToBy: none
  • sentOnInitialization: false
mapboxRetrieveAddress
Retrieve an address - see https://docs.mapbox.com/api/search/search/#retrieve-a-suggested-feature
  • source: block
  • data: object
    propertytyperequireddescription
    suggestionobjectyes
    optionsArgyes
  • errorCodes: none
  • respondedToBy: mapboxRetrieveAddressResponse
  • sentOnInitialization: false
mapboxRetrieveAddressResponse
The response to a retrieve an address
  • source: embedder
  • data: object
    propertytyperequireddescription
    typestringyes
    attributionstringnoThe attribution data for results.
    features[]yesThe returned feature objects.
  • errorCodes: FORBIDDEN, INTERNAL_ERROR, INVALID_INPUT, NOT_IMPLEMENTED, TOO_MANY_REQUESTS, UNAUTHORIZED
  • respondedToBy: none
  • sentOnInitialization: false
mapboxCanRetrieveAddress
Whether an address can be retrieved
  • source: block
  • data: object
    propertytyperequireddescription
    suggestionobjectyes
  • errorCodes: none
  • respondedToBy: mapboxCanRetrieveAddressResponse
  • sentOnInitialization: false
mapboxCanRetrieveAddressResponse
The response to a can retrieve an address request
  • source: embedder
  • data: object
    propertytyperequireddescription
    canRetrieveAddressbooleanyes
  • errorCodes: FORBIDDEN, INTERNAL_ERROR, INVALID_INPUT, NOT_IMPLEMENTED, TOO_MANY_REQUESTS, UNAUTHORIZED
  • respondedToBy: none
  • sentOnInitialization: false
mapboxRetrieveStaticMap
Retrieve a static map – see https://docs.mapbox.com/api/maps/static-images
  • source: block
  • data: object
    propertytyperequireddescription
    usernamestringyes
    style_idstringyes
    overlaystringyes
    lonnumberyes
    latnumberyes
    zoomnumberyes
    bboxyes
    autostringyes
    widthnumberyes
    heightnumberyes
    bearingnumberno
    pitchnumberno
    @2xstringno
    attributionbooleanno
    logobooleanno
    before_layerstringno
    addlayerobjectno
    setfilterstring[]no
    layer_idstringno
    paddingstringno
  • errorCodes: none
  • respondedToBy: mapboxRetrieveStaticMapResponse
  • sentOnInitialization: false
mapboxRetrieveStaticMapResponse
The response to a retrieve static map request
  • source: embedder
  • data: object
    propertytyperequireddescription
    typestringyes
    dataobjectyes
  • errorCodes: FORBIDDEN, INTERNAL_ERROR, INVALID_INPUT, NOT_IMPLEMENTED, TOO_MANY_REQUESTS, UNAUTHORIZED
  • respondedToBy: none
  • sentOnInitialization: false

Previous

Add blocks to your app

Anyone with an existing application who wants to embed semantically-rich, reusable blocks in their product can use the protocol. Improve your app’s utility and tap into a world of structured data with no extra effort, for free.

Learn more

Build your own blocks

Any developer can build and publish blocks to the Hub for others to use. Contribute to an open-source community building a more interoperable future web.

Read the quickstart guide