Skip to navigation

Connect your own account

Use the OAuth 2.0 client credentials grant for server-to-server access to your Middesk account without user sign-in. For access on behalf of a user, use the authorization-code flow.

Getting an access token

1

Create an OAuth client

Use your API key to create a confidential client with client_credentials enabled. These settings are available through the API.

Create an OAuth client
curl https://api.middesk.com/v1/oauth_clients \
-u "$MIDDESK_API_KEY:" \
-H 'Content-Type: application/json' \
-d '{
"name": "My integration",
"redirect_uri": "https://example.com/oauth/unused",
"confidential": true,
"allowed_grant_types": ["client_credentials"],
"access_token_ttl": 600
}'

Set access_token_ttl to the token lifetime in seconds, from 1 to 86400. This example issues tokens that expire after 10 minutes. If you omit it, tokens expire after one hour. To change the lifetime of an existing client, send access_token_ttl to PATCH /v1/oauth_clients/{client_id}; the new lifetime applies to tokens issued afterward.

Replace the example URI with a URL you control. A redirect URI is required to create the client, but this grant does not use it. Store the returned client_id and secret as CLIENT_ID and CLIENT_SECRET on your server. If you already have a client with this grant enabled, use its credentials.

2

Request a token

Send a form-encoded request using the client ID and secret as HTTP Basic Auth credentials.

Request an access token
curl https://api.middesk.com/oauth/token \
-u "$CLIENT_ID:$CLIENT_SECRET" \
-H 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'grant_type=client_credentials' \
--data-urlencode 'scope=read_only'

Choose an access scope; if omitted, scope defaults to read_write. You can send client_id and client_secret in the form body instead of using Basic Auth.

The response uses the shared token fields and includes expires_in. Use expires_in to determine when to request a new token. This grant does not issue refresh tokens.

3

Make an API request

Follow the shared API request example and token restrictions. Machine tokens also cannot invite or remove account users, create or remove affiliate users, or create, remove, or reinvite partner users.

Authenticating with a private key

If your integration uses private_key_jwt, send a signed JWT assertion in place of the client secret.

Create the client with token_endpoint_auth_method: "private_key_jwt" and jwks containing a keys array of public JSON Web Keys. The client must be confidential and allow client_credentials. Give each public key a unique kid and keep private keys on your server.

Use your JWT library to sign an assertion with these values:

FieldValue
Header algUse RS256 with an RSA key.
Header kidThe ID of the registered public key.
iss, subYour client ID.
audhttps://api.middesk.com/oauth/token
iatCurrent Unix time in seconds.
expExpiration time, at most 600 seconds after iat.
jtiA unique value for each request, including retries.
Request a token with a signed assertion
curl https://api.middesk.com/oauth/token \
-H 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'grant_type=client_credentials' \
--data-urlencode 'scope=read_only' \
--data-urlencode 'client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer' \
--data-urlencode "client_assertion=$CLIENT_ASSERTION"

Do not send a client secret with the assertion. Each assertion can be used once; the token response and API usage are the same as above.

Rotating signing keys

A private_key_jwt client can replace its own public keys without an API key, an access token, or a client secret. Sign the request with a key that’s already registered, and Middesk replaces the client’s key set.

1

Generate a new key pair

Create a new key pair on your server and give the public key a kid that isn’t already registered. Keep the current key in use until the new one is registered.

2

Register the new key alongside the current one

Sign an assertion with the current key, using the same fields as a token request but with this aud:

Assertion audience
https://api.middesk.com/oauth/clients/{client_id}/jwks

Send the complete key set you want registered as a JSON string in the jwks field. The set replaces every key on the client, so include the current key and the new one.

Replace the client's public keys
curl -X PUT "https://api.middesk.com/oauth/clients/$CLIENT_ID/jwks" \
-H 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer' \
--data-urlencode "client_assertion=$CLIENT_ASSERTION" \
--data-urlencode "jwks=$JWKS"

The response contains the client_id and the registered jwks.

3

Switch to the new key

Sign token requests with the new key. Both keys can get tokens until you remove the old one.

4

Remove the old key

Send another request signed with the new key, with a jwks that contains only the new key. Assertions signed with the old key are rejected from then on.

Middesk applies these rules to key set changes:

  • The new key set must include the key that signed the request, with the same kid and key material. This prevents a request from removing the key it was signed with.
  • Each assertion can be used once, and an assertion for the token endpoint isn’t accepted here, or the reverse.
  • The key set holds one to five public RSA or EC keys, each with a unique kid. Keys that include private key material are rejected.
  • Only clients that use private_key_jwt can change keys this way. Update a secret-based client with your API key through PATCH /v1/oauth_clients/{client_id}.

A failed assertion returns 401 with invalid_client. A missing jwks returns 400 with invalid_request, and an invalid key set returns 400 with invalid_client_metadata. Both 400 responses include an error_description. Middesk records each successful change as a change_settings security event that lists the old and new kid values.

Revoking access

Use the shared revocation request. For private_key_jwt, replace Basic Auth with client_assertion_type and a fresh client_assertion, using the same audience as the token request.

Disabling the grant or removing a signing key does not invalidate tokens already issued.

Get a demo
Contact your account manager or contact sales to inquire about access.

Next steps