Namespace: microsoft.graph
Add a key credential to an application. This method, along with removeKey can be used by an application to automate rolling its expiring keys.
Note
Create application and
Update application operations can continue to be used to add and update key credentials for any application with or without a user's context.
You should only provide the public key value when adding a certificate credential to your application. Adding a private key certificate to your application risks compromising the application.
As part of the request validation for this method, a proof of possession of an existing key is verified before the action can be performed.
Applications that don’t have any existing valid certificates (no certificates have been added yet, or all certificates have expired), won’t be able to use this service action. You can use the Update application operation to perform an update instead.
This API is available in the following national cloud deployments.
Global service |
US Government L4 |
US Government L5 (DOD) |
China operated by 21Vianet |
✅ |
✅ |
✅ |
✅ |
Permissions
Choose the permission or permissions marked as least privileged for this API. Use a higher privileged permission or permissions only if your app requires it. For details about delegated and application permissions, see Permission types. To learn more about these permissions, see the permissions reference.
Permission type |
Least privileged permissions |
Higher privileged permissions |
Delegated (work or school account) |
Application.ReadWrite.All |
Directory.ReadWrite.All |
Delegated (personal Microsoft account) |
Not supported. |
Not supported. |
Application |
Application.ReadWrite.OwnedBy |
Application.ReadWrite.All, Directory.ReadWrite.All |
Note
An application does not need any specific permission to roll its own keys.
HTTP request
You can address the application using either its id or appId. id and appId are referred to as the Object ID and Application (Client) ID, respectively, in app registrations in the Microsoft Entra admin center.
POST /applications/{id}/addKey
POST /applications(appId='{appId}')/addKey
Request body
In the request body, provide the following required properties.
Property |
Type |
Description |
keyCredential |
keyCredential |
The new application key credential to add. The type, usage and key are required properties for this usage. Supported key types are:
AsymmetricX509Cert : The usage must be Verify .X509CertAndPassword : The usage must be Sign
|
passwordCredential |
passwordCredential |
Only secretText is required to be set which should contain the password for the key. This property is required only for keys of type X509CertAndPassword . Set it to null otherwise. |
proof |
String |
A self-signed JWT token used as a proof of possession of the existing keys. This JWT token must be signed with a private key that corresponds to one of the existing valid certificates associated with the application. The token should contain the following claims:- aud: Audience needs to be
00000002-0000-0000-c000-000000000000 . - iss: Issuer needs to be the ID of the application that initiates the request.
- nbf: Not before time.
- exp: Expiration time should be the value of nbf + 10 minutes.
For steps to generate this proof of possession token, see Generating proof of possession tokens for rolling keys. For more information about the claim types, see Claims payload. |
Response
If successful, this method returns a 200 OK
response code and a new keyCredential object in the response body.
Examples
Example 1: Add a new key credential to an application
Request
The following example shows a request.
POST https://graph.microsoft.com/v1.0/applications/{id}/addKey
Content-type: application/json
{
"keyCredential": {
"type": "AsymmetricX509Cert",
"usage": "Verify",
"key": "MIIDYDCCAki..."
},
"passwordCredential": null,
"proof":"eyJ0eXAiOiJ..."
}
// Code snippets are only available for the latest version. Current version is 5.x
// Dependencies
using Microsoft.Graph.Applications.Item.AddKey;
using Microsoft.Graph.Models;
var requestBody = new AddKeyPostRequestBody
{
KeyCredential = new KeyCredential
{
Type = "AsymmetricX509Cert",
Usage = "Verify",
Key = Convert.FromBase64String("MIIDYDCCAki..."),
},
PasswordCredential = null,
Proof = "eyJ0eXAiOiJ...",
};
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=csharp
var result = await graphClient.Applications["{application-id}"].AddKey.PostAsync(requestBody);
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation.
mgc applications add-key post --application-id {application-id} --body '{\
"keyCredential": {\
"type": "AsymmetricX509Cert",\
"usage": "Verify",\
"key": "MIIDYDCCAki..."\
},\
"passwordCredential": null,\
"proof":"eyJ0eXAiOiJ..."\
}\
'
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation.
// Code snippets are only available for the latest major version. Current major version is $v1.*
// Dependencies
import (
"context"
msgraphsdk "github.com/microsoftgraph/msgraph-sdk-go"
graphapplications "github.com/microsoftgraph/msgraph-sdk-go/applications"
graphmodels "github.com/microsoftgraph/msgraph-sdk-go/models"
//other-imports
)
requestBody := graphapplications.NewAddKeyPostRequestBody()
keyCredential := graphmodels.NewKeyCredential()
type := "AsymmetricX509Cert"
keyCredential.SetType(&type)
usage := "Verify"
keyCredential.SetUsage(&usage)
key := []byte("mIIDYDCCAki...")
keyCredential.SetKey(&key)
requestBody.SetKeyCredential(keyCredential)
passwordCredential := null
requestBody.SetPasswordCredential(&passwordCredential)
proof := "eyJ0eXAiOiJ..."
requestBody.SetProof(&proof)
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=go
addKey, err := graphClient.Applications().ByApplicationId("application-id").AddKey().Post(context.Background(), requestBody, nil)
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation.
// Code snippets are only available for the latest version. Current version is 6.x
GraphServiceClient graphClient = new GraphServiceClient(requestAdapter);
com.microsoft.graph.applications.item.addkey.AddKeyPostRequestBody addKeyPostRequestBody = new com.microsoft.graph.applications.item.addkey.AddKeyPostRequestBody();
KeyCredential keyCredential = new KeyCredential();
keyCredential.setType("AsymmetricX509Cert");
keyCredential.setUsage("Verify");
byte[] key = Base64.getDecoder().decode("MIIDYDCCAki...");
keyCredential.setKey(key);
addKeyPostRequestBody.setKeyCredential(keyCredential);
addKeyPostRequestBody.setPasswordCredential(null);
addKeyPostRequestBody.setProof("eyJ0eXAiOiJ...");
KeyCredential result = graphClient.applications().byApplicationId("{application-id}").addKey().post(addKeyPostRequestBody);
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation.
const options = {
authProvider,
};
const client = Client.init(options);
const keyCredential = {
keyCredential: {
type: 'AsymmetricX509Cert',
usage: 'Verify',
key: 'MIIDYDCCAki...'
},
passwordCredential: null,
proof: 'eyJ0eXAiOiJ...'
};
await client.api('/applications/{id}/addKey')
.post(keyCredential);
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation.
<?php
use Microsoft\Graph\GraphServiceClient;
use Microsoft\Graph\Generated\Applications\Item\AddKey\AddKeyPostRequestBody;
use Microsoft\Graph\Generated\Models\KeyCredential;
$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
$requestBody = new AddKeyPostRequestBody();
$keyCredential = new KeyCredential();
$keyCredential->setType('AsymmetricX509Cert');
$keyCredential->setUsage('Verify');
$keyCredential->setKey(\GuzzleHttp\Psr7\Utils::streamFor(base64_decode('MIIDYDCCAki...')));
$requestBody->setKeyCredential($keyCredential);
$requestBody->setPasswordCredential(null);
$requestBody->setProof('eyJ0eXAiOiJ...');
$result = $graphServiceClient->applications()->byApplicationId('application-id')->addKey()->post($requestBody)->wait();
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation.
Import-Module Microsoft.Graph.Applications
$params = @{
keyCredential = @{
type = "AsymmetricX509Cert"
usage = "Verify"
key = [System.Text.Encoding]::ASCII.GetBytes("MIIDYDCCAki...")
}
passwordCredential = $null
proof = "eyJ0eXAiOiJ..."
}
Add-MgApplicationKey -ApplicationId $applicationId -BodyParameter $params
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation.
# Code snippets are only available for the latest version. Current version is 1.x
from msgraph import GraphServiceClient
from msgraph.generated.applications.item.add_key.add_key_post_request_body import AddKeyPostRequestBody
from msgraph.generated.models.key_credential import KeyCredential
# To initialize your graph_client, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=python
request_body = AddKeyPostRequestBody(
key_credential = KeyCredential(
type = "AsymmetricX509Cert",
usage = "Verify",
key = base64.urlsafe_b64decode("MIIDYDCCAki..."),
),
password_credential = None,
proof = "eyJ0eXAiOiJ...",
)
result = await graph_client.applications.by_application_id('application-id').add_key.post(request_body)
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation.
Response
The following example shows the response.
HTTP/1.1 200 OK
Content-Type: application/json
{
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#microsoft.graph.keyCredential"
}
Example 2: Add a key credential and an associated password for the key
Request
The following example shows a request.
POST https://graph.microsoft.com/v1.0/applications/{id}/addKey
Content-type: application/json
{
"keyCredential": {
"type": "X509CertAndPassword",
"usage": "Sign",
"key": "MIIDYDCCAki..."
},
"passwordCredential": {
"secretText": "MKTr0w1..."
},
"proof":"eyJ0eXAiOiJ..."
}
// Code snippets are only available for the latest version. Current version is 5.x
// Dependencies
using Microsoft.Graph.Applications.Item.AddKey;
using Microsoft.Graph.Models;
var requestBody = new AddKeyPostRequestBody
{
KeyCredential = new KeyCredential
{
Type = "X509CertAndPassword",
Usage = "Sign",
Key = Convert.FromBase64String("MIIDYDCCAki..."),
},
PasswordCredential = new PasswordCredential
{
SecretText = "MKTr0w1...",
},
Proof = "eyJ0eXAiOiJ...",
};
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=csharp
var result = await graphClient.Applications["{application-id}"].AddKey.PostAsync(requestBody);
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation.
mgc applications add-key post --application-id {application-id} --body '{\
"keyCredential": {\
"type": "X509CertAndPassword",\
"usage": "Sign",\
"key": "MIIDYDCCAki..."\
},\
"passwordCredential": {\
"secretText": "MKTr0w1..."\
},\
"proof":"eyJ0eXAiOiJ..."\
}\
'
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation.
// Code snippets are only available for the latest major version. Current major version is $v1.*
// Dependencies
import (
"context"
msgraphsdk "github.com/microsoftgraph/msgraph-sdk-go"
graphapplications "github.com/microsoftgraph/msgraph-sdk-go/applications"
graphmodels "github.com/microsoftgraph/msgraph-sdk-go/models"
//other-imports
)
requestBody := graphapplications.NewAddKeyPostRequestBody()
keyCredential := graphmodels.NewKeyCredential()
type := "X509CertAndPassword"
keyCredential.SetType(&type)
usage := "Sign"
keyCredential.SetUsage(&usage)
key := []byte("mIIDYDCCAki...")
keyCredential.SetKey(&key)
requestBody.SetKeyCredential(keyCredential)
passwordCredential := graphmodels.NewPasswordCredential()
secretText := "MKTr0w1..."
passwordCredential.SetSecretText(&secretText)
requestBody.SetPasswordCredential(passwordCredential)
proof := "eyJ0eXAiOiJ..."
requestBody.SetProof(&proof)
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=go
addKey, err := graphClient.Applications().ByApplicationId("application-id").AddKey().Post(context.Background(), requestBody, nil)
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation.
// Code snippets are only available for the latest version. Current version is 6.x
GraphServiceClient graphClient = new GraphServiceClient(requestAdapter);
com.microsoft.graph.applications.item.addkey.AddKeyPostRequestBody addKeyPostRequestBody = new com.microsoft.graph.applications.item.addkey.AddKeyPostRequestBody();
KeyCredential keyCredential = new KeyCredential();
keyCredential.setType("X509CertAndPassword");
keyCredential.setUsage("Sign");
byte[] key = Base64.getDecoder().decode("MIIDYDCCAki...");
keyCredential.setKey(key);
addKeyPostRequestBody.setKeyCredential(keyCredential);
PasswordCredential passwordCredential = new PasswordCredential();
passwordCredential.setSecretText("MKTr0w1...");
addKeyPostRequestBody.setPasswordCredential(passwordCredential);
addKeyPostRequestBody.setProof("eyJ0eXAiOiJ...");
KeyCredential result = graphClient.applications().byApplicationId("{application-id}").addKey().post(addKeyPostRequestBody);
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation.
const options = {
authProvider,
};
const client = Client.init(options);
const keyCredential = {
keyCredential: {
type: 'X509CertAndPassword',
usage: 'Sign',
key: 'MIIDYDCCAki...'
},
passwordCredential: {
secretText: 'MKTr0w1...'
},
proof: 'eyJ0eXAiOiJ...'
};
await client.api('/applications/{id}/addKey')
.post(keyCredential);
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation.
<?php
use Microsoft\Graph\GraphServiceClient;
use Microsoft\Graph\Generated\Applications\Item\AddKey\AddKeyPostRequestBody;
use Microsoft\Graph\Generated\Models\KeyCredential;
use Microsoft\Graph\Generated\Models\PasswordCredential;
$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
$requestBody = new AddKeyPostRequestBody();
$keyCredential = new KeyCredential();
$keyCredential->setType('X509CertAndPassword');
$keyCredential->setUsage('Sign');
$keyCredential->setKey(\GuzzleHttp\Psr7\Utils::streamFor(base64_decode('MIIDYDCCAki...')));
$requestBody->setKeyCredential($keyCredential);
$passwordCredential = new PasswordCredential();
$passwordCredential->setSecretText('MKTr0w1...');
$requestBody->setPasswordCredential($passwordCredential);
$requestBody->setProof('eyJ0eXAiOiJ...');
$result = $graphServiceClient->applications()->byApplicationId('application-id')->addKey()->post($requestBody)->wait();
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation.
Import-Module Microsoft.Graph.Applications
$params = @{
keyCredential = @{
type = "X509CertAndPassword"
usage = "Sign"
key = [System.Text.Encoding]::ASCII.GetBytes("MIIDYDCCAki...")
}
passwordCredential = @{
secretText = "MKTr0w1..."
}
proof = "eyJ0eXAiOiJ..."
}
Add-MgApplicationKey -ApplicationId $applicationId -BodyParameter $params
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation.
# Code snippets are only available for the latest version. Current version is 1.x
from msgraph import GraphServiceClient
from msgraph.generated.applications.item.add_key.add_key_post_request_body import AddKeyPostRequestBody
from msgraph.generated.models.key_credential import KeyCredential
from msgraph.generated.models.password_credential import PasswordCredential
# To initialize your graph_client, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=python
request_body = AddKeyPostRequestBody(
key_credential = KeyCredential(
type = "X509CertAndPassword",
usage = "Sign",
key = base64.urlsafe_b64decode("MIIDYDCCAki..."),
),
password_credential = PasswordCredential(
secret_text = "MKTr0w1...",
),
proof = "eyJ0eXAiOiJ...",
)
result = await graph_client.applications.by_application_id('application-id').add_key.post(request_body)
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation.
Response
The following example shows the response.
HTTP/1.1 200 OK
Content-Type: application/json
{
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#microsoft.graph.keyCredential"
}