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 SQL Server Downloads
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @iTmp0 int
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- 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).
DECLARE @sbJwk int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbJwk OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
EXEC sp_OAMethod @sbJwk, 'LoadFile', @success OUT, 'qa_data/enthuse/client_key.jwk', 'utf-8'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @sbJwk, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @sbJwk
RETURN
END
DECLARE @clientKey int
EXEC @hr = sp_OACreate 'Chilkat.PrivateKey', @clientKey OUT
EXEC sp_OAMethod @sbJwk, 'GetAsString', @sTmp0 OUT
EXEC sp_OAMethod @clientKey, 'LoadJwk', @success OUT, @sTmp0
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @clientKey, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @sbJwk
EXEC @hr = sp_OADestroy @clientKey
RETURN
END
-- Get the key ID (kid) from the JWK.
DECLARE @jwk int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jwk OUT
EXEC sp_OAMethod @sbJwk, 'GetAsString', @sTmp0 OUT
EXEC sp_OAMethod @jwk, 'Load', @success OUT, @sTmp0
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @jwk, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @sbJwk
EXEC @hr = sp_OADestroy @clientKey
EXEC @hr = sp_OADestroy @jwk
RETURN
END
DECLARE @kid nvarchar(4000)
EXEC sp_OAMethod @jwk, 'StringOf', @kid OUT, 'kid'
DECLARE @http int
EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
-- 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.
EXEC sp_OASetProperty @http, 'UserAgent', '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.
EXEC sp_OAMethod @http, 'SetClientAssertionKey', @success OUT, @clientKey, @kid
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @sbJwk
EXEC @hr = sp_OADestroy @clientKey
EXEC @hr = sp_OADestroy @jwk
EXEC @hr = sp_OADestroy @http
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).
DECLARE @rsa int
EXEC @hr = sp_OACreate 'Chilkat.Rsa', @rsa OUT
DECLARE @dpopKey int
EXEC @hr = sp_OACreate 'Chilkat.PrivateKey', @dpopKey OUT
EXEC sp_OAMethod @rsa, 'GenKey', @success OUT, 2048, @dpopKey
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @rsa, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @sbJwk
EXEC @hr = sp_OADestroy @clientKey
EXEC @hr = sp_OADestroy @jwk
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @rsa
EXEC @hr = sp_OADestroy @dpopKey
RETURN
END
EXEC sp_OAMethod @http, 'SetDPoPKey', @success OUT, @dpopKey
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @sbJwk
EXEC @hr = sp_OADestroy @clientKey
EXEC @hr = sp_OADestroy @jwk
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @rsa
EXEC @hr = sp_OADestroy @dpopKey
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.
DECLARE @json int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'client_id', 'ab12cd34-ef56-7890-ab12-cd34ef567890'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'org_id', '123'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'scope', 'org.read org.pii'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'resource', 'https://api.enthuse.cloud'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'token_endpoint', 'https://identity.enthuse.cloud/oidc/token'
EXEC sp_OAMethod @json, 'Emit', @sTmp0 OUT
EXEC sp_OASetProperty @http, 'AuthToken', @sTmp0
EXEC sp_OASetProperty @http, 'Accept', '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.
DECLARE @resp int
EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT
EXEC sp_OAMethod @http, 'HttpNoBody', @success OUT, 'POST', 'https://api.enthuse.cloud/v3/payments?since=2026-08-01T00:00:00Z', @resp
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @sbJwk
EXEC @hr = sp_OADestroy @clientKey
EXEC @hr = sp_OADestroy @jwk
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @rsa
EXEC @hr = sp_OADestroy @dpopKey
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @resp
RETURN
END
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
PRINT 'Response status code = ' + @iTmp0
-- The data endpoints return NDJSON: one JSON object per line, then a final "_meta" line.
EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @sbJwk
EXEC @hr = sp_OADestroy @clientKey
EXEC @hr = sp_OADestroy @jwk
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @rsa
EXEC @hr = sp_OADestroy @dpopKey
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @resp
END
GO