Sample code for 30+ languages & platforms
DataFlex Requires Chilkat v11.7.0+

Enthuse API: DPoP Access Token with a Client Assertion (private_key_jwt)

See more OAuth2 Examples

Demonstrates how to get a DPoP-bound access token from the Enthuse API with the OAuth2 client credentials grant, and call the API. Enthuse authenticates the client with a client assertion (private_key_jwt): a short-lived JWT signed with the private key of the client, which must be created anew for each token request. The access token is bound to a DPoP key (RFC 9449), so each request also needs a new DPoP proof.

Chilkat does all of this automatically: SetClientAssertionKey provides the key for the client assertion, SetDPoPKey provides the DPoP key, and the AuthToken property provides the client credentials. Chilkat requests the access token when it is needed, sends the Authorization: DPoP and DPoP headers with each API request, and requests a new token when the current one is about to expire.

Note: Requires Chilkat v11.7.0 or later.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoSbJwk
    Variant vClientKey
    Handle hoClientKey
    Handle hoJwk
    String sKid
    Handle hoHttp
    Handle hoRsa
    Variant vDpopKey
    Handle hoDpopKey
    Handle hoJson
    Variant vResp
    Handle hoResp
    String sTemp1
    Integer iTemp1

    Move False To iSuccess

    //  This example requires Chilkat v11.7.0 or later.

    //  It gets an access token from Enthuse with the OAuth2 client credentials grant, using a
    //  client assertion (private_key_jwt) for client authentication and DPoP for the token binding,
    //  and then calls the Enthuse API.  Chilkat creates the client assertion and the DPoP proofs.

    //  Load the client private key.  Enthuse Cloud gives it to you as a JWK file when you create
    //  the client (Integration -> API -> Create Client).
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbJwk
    If (Not(IsComObjectCreated(hoSbJwk))) Begin
        Send CreateComObject of hoSbJwk
    End
    Get ComLoadFile Of hoSbJwk "qa_data/enthuse/client_key.jwk" "utf-8" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSbJwk To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatPrivateKey)) To hoClientKey
    If (Not(IsComObjectCreated(hoClientKey))) Begin
        Send CreateComObject of hoClientKey
    End
    Get ComGetAsString Of hoSbJwk To sTemp1
    Get ComLoadJwk Of hoClientKey sTemp1 To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoClientKey To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Get the key ID (kid) from the JWK.
    Get Create (RefClass(cComChilkatJsonObject)) To hoJwk
    If (Not(IsComObjectCreated(hoJwk))) Begin
        Send CreateComObject of hoJwk
    End
    Get ComGetAsString Of hoSbJwk To sTemp1
    Get ComLoad Of hoJwk sTemp1 To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoJwk To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComStringOf Of hoJwk "kid" To sKid

    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End

    //  Enthuse production is behind Cloudflare, which rejects some default HTTP user agents.
    //  Send a User-Agent that identifies your integration.  Chilkat sends it with the token
    //  request and with every API call.
    Set ComUserAgent Of hoHttp To "ExampleCharity-CRM/1.0"

    //  Provide the client key for the client assertion.  For each token request, Chilkat creates
    //  a new client_assertion JWT signed with this key, with the claims iss and sub (the client_id),
    //  aud (the token_endpoint), iat, exp (5 minutes after iat) and a unique jti, and the kid
    //  in its header.  It is sent with client_assertion_type set to
    //  urn:ietf:params:oauth:client-assertion-type:jwt-bearer.
    Get pvComObject of hoClientKey to vClientKey
    Get ComSetClientAssertionKey Of hoHttp vClientKey sKid To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoHttp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Provide the DPoP key.  It can be the client key or a separate key, but the same DPoP key
    //  must be used for the token request and for the API calls.  This example generates a
    //  separate RSA key (the DPoP proofs are then signed with RS256).
    Get Create (RefClass(cComChilkatRsa)) To hoRsa
    If (Not(IsComObjectCreated(hoRsa))) Begin
        Send CreateComObject of hoRsa
    End
    Get Create (RefClass(cComChilkatPrivateKey)) To hoDpopKey
    If (Not(IsComObjectCreated(hoDpopKey))) Begin
        Send CreateComObject of hoDpopKey
    End
    Get pvComObject of hoDpopKey to vDpopKey
    Get ComGenKey Of hoRsa 2048 vDpopKey To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoRsa To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get pvComObject of hoDpopKey to vDpopKey
    Get ComSetDPoPKey Of hoHttp vDpopKey To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoHttp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Provide the details for the client credentials token request.  Every member except
    //  token_endpoint is sent as a form field.  Use the client_id and org_id issued by
    //  the Enthuse environment you call.  For the development environment, use
    //  https://identity.enthuse-test.com/oidc/token and https://api.enthuse-test.com instead.
    Get Create (RefClass(cComChilkatJsonObject)) To hoJson
    If (Not(IsComObjectCreated(hoJson))) Begin
        Send CreateComObject of hoJson
    End
    Get ComUpdateString Of hoJson "client_id" "ab12cd34-ef56-7890-ab12-cd34ef567890" To iSuccess
    Get ComUpdateString Of hoJson "org_id" "123" To iSuccess
    Get ComUpdateString Of hoJson "scope" "org.read org.pii" To iSuccess
    Get ComUpdateString Of hoJson "resource" "https://api.enthuse.cloud" To iSuccess
    Get ComUpdateString Of hoJson "token_endpoint" "https://identity.enthuse.cloud/oidc/token" To iSuccess
    Get ComEmit Of hoJson To sTemp1
    Set ComAuthToken Of hoHttp To sTemp1

    Set ComAccept Of hoHttp To "application/json"

    //  Call the API.  There is no access token yet, so Chilkat first sends the token request
    //  to the token_endpoint, with the client assertion and a DPoP proof for POST to the token
    //  endpoint.  Chilkat then sends the API request with these headers:

    //  Authorization: DPoP <access_token>
    //  DPoP: <a new DPoP proof for POST https://api.enthuse.cloud/v3/payments, with the ath claim>

    //  The proof's htu claim is the URL without its query string.
    //  Enthuse access tokens last 600 seconds.  Chilkat reuses the token for later requests made
    //  with the same Http object, with a new DPoP proof for each request, and gets a new token
    //  (with a new client assertion) when the token is about to expire.

    Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
    If (Not(IsComObjectCreated(hoResp))) Begin
        Send CreateComObject of hoResp
    End
    Get pvComObject of hoResp to vResp
    Get ComHttpNoBody Of hoHttp "POST" "https://api.enthuse.cloud/v3/payments?since=2026-08-01T00:00:00Z" vResp To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoHttp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComStatusCode Of hoResp To iTemp1
    Showln "Response status code = " iTemp1

    //  The data endpoints return NDJSON: one JSON object per line, then a final "_meta" line.
    Get ComBodyStr Of hoResp To sTemp1
    Showln sTemp1


End_Procedure