safeLink Create a valid (by construction) relative URI with query params.
This function will only typecheck if endpoint is part of the API api
:: a typeCtrl KGHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27
Moduleservant-0.20.2Haskell2010
Type safe generation of internal links.
Given an API with a few endpoints:
:set -XDataKinds -XTypeFamilies -XTypeOperators -XPolyKindsimport Servant.APIimport Servant.Linksimport Web.HttpApiData (toUrlPiece)import Data.Proxytype Hello = "hello" :> Get '[JSON] Inttype Bye = "bye" :> QueryParam "name" String :> Delete '[JSON] NoContenttype API = Hello :<|> Byelet api = Proxy :: Proxy API
It is possible to generate links that are guaranteed to be within API with
safeLink. The first argument to safeLink is a type representing the API
you would like to restrict links to. The second argument is the destination
endpoint you would like the link to point to, this will need to end with a
verb like GET or POST. Further arguments may be required depending on the
type of the endpoint. If everything lines up you will get a Link out the
other end.
You may omit QueryParams and the like should you not want to provide them,
but types which form part of the URL path like Capture must be included.
The reason you may want to omit QueryParams is that safeLink is a bit
magical: if parameters are included that could take input it will return a
function that accepts that input and generates a link. This is best shown
with an example. Here, a link is generated with no parameters:
let hello = Proxy :: Proxy ("hello" :> Get '[JSON] Int)toUrlPiece (safeLink api hello :: Link)"hello"
If the API has an endpoint with parameters then we can generate links with or without those:
let with = Proxy :: Proxy ("bye" :> QueryParam "name" String :> Delete '[JSON] NoContent)toUrlPiece $ safeLink api with (Just "Hubert")"bye?name=Hubert"
let without = Proxy :: Proxy ("bye" :> Delete '[JSON] NoContent)toUrlPiece $ safeLink api without"bye"
If you would like to create a helper for generating links only within that API, you can partially apply safeLink if you specify a correct type signature like so:
:set -XConstraintKinds:{let apiLink :: (IsElem endpoint API, HasLink endpoint) => Proxy endpoint -> MkLink endpoint Link apiLink = safeLink api:}
safeLink' allows you to specialise the output:
safeLink' toUrlPiece api without"bye"
:{let apiTextLink :: (IsElem endpoint API, HasLink endpoint) => Proxy endpoint -> MkLink endpoint Text apiTextLink = safeLink' toUrlPiece api:}
apiTextLink without"bye"
Attempting to construct a link to an endpoint that does not exist in api will result in a type error like this:
let bad_link = Proxy :: Proxy ("hello" :> Delete '[JSON] NoContent)safeLink api bad_link......Could not ......
This error is essentially saying that the type family couldn't find bad_link under api after trying the open (but empty) type family IsElem' as a last resort.
module Servant.API.TypeLevel
Note that URI is from the Network.URI module in the network-uri package.
safeLink Create a valid (by construction) relative URI with query params.
This function will only typecheck if endpoint is part of the API api
safeLink' More general safeLink.
Create all links in an API.
Note that the api type must be restricted to the endpoints that have
valid links to them.
type API = "foo" :> Capture "name" Text :> Get '[JSON] Text :<|> "bar" :> Capture "name" Int :> Get '[JSON] Doublelet fooLink :<|> barLink = allLinks (Proxy :: Proxy API):t fooLinkfooLink :: Text -> Link:t barLinkbarLink :: Int -> Link
Note: nested APIs don't work well with this approach
:kind! MkLink (Capture "nest" Char :> (Capture "x" Int :> Get '[JSON] Int :<|> Capture "y" Double :> Get '[JSON] Double)) LinkMkLink (Capture "nest" Char :> (Capture "x" Int :> Get '[JSON] Int :<|> Capture "y" Double :> Get '[JSON] Double)) Link :: Type= Char -> (Int -> Link) :<|> (Double -> Link)
Represents a general universal resource identifier using its component parts.
For example, for the URI
foo://anonymous@www.haskell.org:42/ghc?query#fragthe components are:
Eq URIDefined in network-uri-2.6.4.2 · Network.URIData URIDefined in network-uri-2.6.4.2 · Network.URIOrd URIDefined in network-uri-2.6.4.2 · Network.URIShow URIDefined in network-uri-2.6.4.2 · Network.URIGeneric URIDefined in network-uri-2.6.4.2 · Network.URINFData URIDefined in network-uri-2.6.4.2 · Network.URIFromJSON URIDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONFromJSONKey URIDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONToJSON URIDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONToJSONKey URIDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONLift URIDefined in network-uri-2.6.4.2 · Network.URItype Rep URI = D1 ('MetaData "URI"
"Network.URI"
"network-uri-2.6.4.2-7IyyRK4Oj6pFyOdiX4bVqe"
'False) (C1 ('MetaCons "URI"
'PrefixI 'True) ((S1 ('MetaSel ('Just "uriScheme"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 String) :*: S1 ('MetaSel ('Just "uriAuthority"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (Maybe URIAuth))) :*: (S1 ('MetaSel ('Just "uriPath"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 String) :*: (S1 ('MetaSel ('Just "uriQuery"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 String) :*: S1 ('MetaSel ('Just "uriFragment"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 String)))))Defined in network-uri-2.6.4.2 · Network.URIA type that specifies that an API record contains a set of links.
GenericMode (AsLink a)Defined in servant-0.20.2 · Servant.Linkstype (:-) (AsLink a) api = MkLink api aDefined in servant-0.20.2 · Servant.LinksMore general version of fieldLink
allFieldLinks :: (HasLink (ToServantApi routes), GenericServant routes (AsLink Link), ToServant routes (AsLink Link) ~ MkLink (ToServantApi routes) Link) => routes (AsLink Link)Get all links as a record.
More general version of allFieldLinks.
Construct a toLink for an endpoint.
type family MkLink (endpoint :: k) aTypeError (NoInstanceFor (HasLink api)) => HasLink apiDefined in servant-0.20.2 · Servant.LinksHasLink EmptyAPIDefined in servant-0.20.2 · Servant.LinksHasLink RawDefined in servant-0.20.2 · Servant.LinksHasLink RawMDefined in servant-0.20.2 · Servant.Links(HasLink (ToServantApi routes), forall a. GLink routes a, ErrorIfNoGeneric routes) => HasLink (NamedRoutes routes)Defined in servant-0.20.2 · Servant.LinksHasLink (NoContentVerb m)Defined in servant-0.20.2 · Servant.Links(HasLink a, HasLink b) => HasLink (a :<|> b)Defined in servant-0.20.2 · Servant.LinksTypeError (NoInstanceForSub HasLink ty) => HasLink (ty :> sub)Defined in servant-0.20.2 · Servant.LinksTypeError (PartialApplication HasLink arr) => HasLink (arr :> sub)Defined in servant-0.20.2 · Servant.LinksHasLink (UVerb m ct a)Defined in servant-0.20.2 · Servant.LinksHasLink sub => HasLink (HttpVersion :> sub)Defined in servant-0.20.2 · Servant.LinksHasLink sub => HasLink (BasicAuth realm a :> sub)Defined in servant-0.20.2 · Servant.LinksHasLink sub => HasLink (Description s :> sub)Defined in servant-0.20.2 · Servant.LinksHasLink sub => HasLink (Summary s :> sub)Defined in servant-0.20.2 · Servant.LinksHasLink sub => HasLink (AuthProtect tag :> sub)Defined in servant-0.20.2 · Servant.LinksHasLink sub => HasLink (Header' mods sym a :> sub)Defined in servant-0.20.2 · Servant.LinksHasLink sub => HasLink (IsSecure :> sub)Defined in servant-0.20.2 · Servant.LinksHasLink sub => HasLink (RemoteHost :> sub)Defined in servant-0.20.2 · Servant.LinksHasLink sub => HasLink (ReqBody' mods ct a :> sub)Defined in servant-0.20.2 · Servant.LinksHasLink sub => HasLink (StreamBody' mods framing ct a :> sub)Defined in servant-0.20.2 · Servant.LinksHasLink sub => HasLink (WithResource res :> sub)Defined in servant-0.20.2 · Servant.LinksHasLink sub => HasLink (Vault :> sub)Defined in servant-0.20.2 · Servant.Links(KnownSymbol sym, ToHttpApiData v, HasLink sub) => HasLink (QueryParams sym v :> sub)Defined in servant-0.20.2 · Servant.Links(KnownSymbol sym, ToHttpApiData v, HasLink sub, SBoolI (FoldRequired mods)) => HasLink (QueryParam' mods sym v :> sub)Defined in servant-0.20.2 · Servant.Links(KnownSymbol sym, HasLink sub) => HasLink (QueryFlag sym :> sub)Defined in servant-0.20.2 · Servant.Links(KnownSymbol sym, HasLink sub) => HasLink (sym :> sub)Defined in servant-0.20.2 · Servant.Links(ToHttpApiData v, HasLink sub) => HasLink (Capture' mods sym v :> sub)Defined in servant-0.20.2 · Servant.Links(ToHttpApiData v, HasLink sub) => HasLink (CaptureAll sym v :> sub)Defined in servant-0.20.2 · Servant.Links(HasLink sub, ToHttpApiData v) => HasLink (Fragment v :> sub)Defined in servant-0.20.2 · Servant.LinksHasLink sub => HasLink (WithNamedContext name context sub)Defined in servant-0.20.2 · Servant.LinksHasLink (Verb m s ct a)Defined in servant-0.20.2 · Servant.LinksHasLink (Stream m status fr ct a)Defined in servant-0.20.2 · Servant.LinksShow LinkDefined in servant-0.20.2 · Servant.LinksToHttpApiData LinkDefined in servant-0.20.2 · Servant.Linkstype API = "something" :> Get '[JSON] IntlinkURI $ safeLink (Proxy :: Proxy API) (Proxy :: Proxy API)something
type API = "sum" :> QueryParams "x" Int :> Get '[JSON] IntlinkURI $ safeLink (Proxy :: Proxy API) (Proxy :: Proxy API) [1, 2, 3]sum?x[]=1&x[]=2&x[]=3
type API = "foo/bar" :> Get '[JSON] IntlinkURI $ safeLink (Proxy :: Proxy API) (Proxy :: Proxy API)foo%2Fbar
type SomeRoute = "abc" :> Capture "email" String :> Put '[JSON] ()let someRoute = Proxy :: Proxy SomeRoutesafeLink someRoute someRoute "test@example.com"Link {_segments = ["abc","test%40example.com"], _queryParams = [], _fragment = Nothing}
linkURI $ safeLink someRoute someRoute "test@example.com"abc/test%40example.com
Configurable linkURI.
type API = "sum" :> QueryParams "x" Int :> Get '[JSON] IntlinkURI' LinkArrayElementBracket $ safeLink (Proxy :: Proxy API) (Proxy :: Proxy API) [1, 2, 3]sum?x[]=1&x[]=2&x[]=3
linkURI' LinkArrayElementPlain $ safeLink (Proxy :: Proxy API) (Proxy :: Proxy API) [1, 2, 3]sum?x=1&x=2&x=3
How to encode array query elements.
LinkArrayElementBracketfoo[]=1&foo[]=2LinkArrayElementPlainfoo=1&foo=2Bounded LinkArrayElementStyleDefined in servant-0.20.2 · Servant.LinksEnum LinkArrayElementStyleDefined in servant-0.20.2 · Servant.LinksEq LinkArrayElementStyleDefined in servant-0.20.2 · Servant.LinksOrd LinkArrayElementStyleDefined in servant-0.20.2 · Servant.LinksShow LinkArrayElementStyleDefined in servant-0.20.2 · Servant.LinksQuery parameter.