# Credential

# Credential

Artifact:[androidx.credentials:credentials](https://developer.android.com/jetpack/androidx/releases/credentials)  
[View Source](https://cs.android.com/search?q=file:androidx/credentials/Credential.kt+class:androidx.credentials.Credential)  
Added in[1.2.0](https://developer.android.com/jetpack/androidx/releases/credentials#1.2.0)

*** ** * ** ***

Kotlin\|[Java](https://developer.android.com/reference/androidx/credentials/Credential "View this page in Java")

<br />

```
abstract class Credential
```

<br />

Known direct subclasses  
[CustomCredential](https://developer.android.com/reference/kotlin/androidx/credentials/CustomCredential),[DigitalCredential](https://developer.android.com/reference/kotlin/androidx/credentials/DigitalCredential),[PasswordCredential](https://developer.android.com/reference/kotlin/androidx/credentials/PasswordCredential),[PublicKeyCredential](https://developer.android.com/reference/kotlin/androidx/credentials/PublicKeyCredential),[RestoreCredential](https://developer.android.com/reference/kotlin/androidx/credentials/RestoreCredential)  

|----------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------|
| [CustomCredential](https://developer.android.com/reference/kotlin/androidx/credentials/CustomCredential)       | Base class for a custom credential with which the user consented to authenticate to the app.   |
| [DigitalCredential](https://developer.android.com/reference/kotlin/androidx/credentials/DigitalCredential)     | Represents the user's digital credential, generally used for verification or sign-in purposes. |
| [PasswordCredential](https://developer.android.com/reference/kotlin/androidx/credentials/PasswordCredential)   | Represents the user's password credential granted by the user for app sign-in.                 |
| [PublicKeyCredential](https://developer.android.com/reference/kotlin/androidx/credentials/PublicKeyCredential) | Represents the user's passkey credential granted by the user for app sign-in.                  |
| [RestoreCredential](https://developer.android.com/reference/kotlin/androidx/credentials/RestoreCredential)     | Represents the user's restore credential for the app sign in.                                  |

*** ** * ** ***

Base class for a credential with which the user consented to authenticate to the app.  

```kotlin
import androidx.credentials.Credential
import androidx.credentials.CustomCredential
import androidx.credentials.PasswordCredential
import androidx.credentials.PublicKeyCredential

when (credential) {
    is PublicKeyCredential -> {
        val responseJson = credential.authenticationResponseJson
        val userCredential = fidoAuthenticateWithServer(responseJson)
        loginWithPasskey(userCredential)
    }
    is PasswordCredential -> {
        val userName = credential.id
        val password = credential.password
        loginWithPassword(userName, password)
    }
    is CustomCredential -> {
        // If you are also using any external sign-in libraries, parse them here with the
        // utility functions provided they provided.
        if (credential.type == ExampleCustomCredential.TYPE) {
            try {
                val exampleCustomCredential =
                    ExampleCustomCredential.createFrom(credential.data)
                loginWithExampleCustomCredential(exampleCustomCredential)
            } catch (e: ExampleCustomCredential.ExampleCustomCredentialParsingException) {
                // Unlikely to happen. If it does, you likely need to update the dependency
                // version of your external sign-in library.
                Log.e(TAG, "Failed to parse an ExampleCustomCredential", e)
            }
        } else {
            Log.w(
                TAG,
                "Received unrecognized credential type ${credential.type}. " +
                    "This shouldn't happen",
            )
        }
    }
    else -> {
        Log.w(
            TAG,
            "Received unrecognized credential type ${credential.type}. This shouldn't happen",
        )
    }
}
```

## Summary

|                                ### Public companion functions                                |
|----------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Credential](https://developer.android.com/reference/kotlin/androidx/credentials/Credential) | `@`[RequiresApi](https://developer.android.com/reference/kotlin/androidx/annotation/RequiresApi)`(value = 34)` [createFrom](https://developer.android.com/reference/kotlin/androidx/credentials/Credential#createFrom(android.credentials.Credential))`(credential: `[Credential](https://developer.android.com/reference/android/credentials/Credential.html)`)` Parses the[credential](https://developer.android.com/reference/kotlin/androidx/credentials/Credential.Companion#createFrom(android.credentials.Credential))into an instance of[Credential](https://developer.android.com/reference/kotlin/androidx/credentials/Credential). |
| [Credential](https://developer.android.com/reference/kotlin/androidx/credentials/Credential) | [createFrom](https://developer.android.com/reference/kotlin/androidx/credentials/Credential#createFrom(kotlin.String,android.os.Bundle))`(type: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`, data: `[Bundle](https://developer.android.com/reference/android/os/Bundle.html)`)` Parses the raw data into an instance of[Credential](https://developer.android.com/reference/kotlin/androidx/credentials/Credential).                                                                                                                                                                                  |

|                               ### Public properties                               |
|-----------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Bundle](https://developer.android.com/reference/android/os/Bundle.html)          | [data](https://developer.android.com/reference/kotlin/androidx/credentials/Credential#data()) the credential data in the[Bundle](https://developer.android.com/reference/android/os/Bundle.html)format                                                                                                                                                                                                                                                                                                                                                              |
| [String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html) | [type](https://developer.android.com/reference/kotlin/androidx/credentials/Credential#type()) the credential type determined by the credential-type-specific subclass (e.g.[PasswordCredential.TYPE_PASSWORD_CREDENTIAL](https://developer.android.com/reference/kotlin/androidx/credentials/PasswordCredential#TYPE_PASSWORD_CREDENTIAL())for`PasswordCredential`or[PublicKeyCredential.TYPE_PUBLIC_KEY_CREDENTIAL](https://developer.android.com/reference/kotlin/androidx/credentials/PublicKeyCredential#TYPE_PUBLIC_KEY_CREDENTIAL())for`PublicKeyCredential`) |

## Public companion functions

### createFrom

Added in[1.5.0](https://developer.android.com/jetpack/androidx/releases/credentials#1.5.0)  

```
@RequiresApi(value = 34)
fun createFrom(credential: Credential): Credential
```

Parses the[credential](https://developer.android.com/reference/kotlin/androidx/credentials/Credential.Companion#createFrom(android.credentials.Credential))into an instance of[Credential](https://developer.android.com/reference/kotlin/androidx/credentials/Credential).

It is recommended to construct a Credential by directly instantiating a Credential subclass, instead of using this API. This API should only be used by a small subset of system apps that reconstruct an existing object for user interactions such as collecting consents.  

|                                               Parameters                                                |
|---------------------------------------------------------------------------------------------------------|---------------------------------|
| `credential: `[Credential](https://developer.android.com/reference/android/credentials/Credential.html) | the framework Credential object |

### createFrom

Added in[1.5.0](https://developer.android.com/jetpack/androidx/releases/credentials#1.5.0)  

```
fun createFrom(type: String, data: Bundle): Credential
```

Parses the raw data into an instance of[Credential](https://developer.android.com/reference/kotlin/androidx/credentials/Credential).

It is recommended to construct a Credential by directly instantiating a Credential subclass, instead of using this API. This API should only be used by a small subset of system apps that reconstruct an existing object for user interactions such as collecting consents.  

|                                        Parameters                                         |
|-------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `type: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html) | matches[Credential.type](https://developer.android.com/reference/kotlin/androidx/credentials/Credential#type()), the credential type                                                                                                                                                                                                                                                                                     |
| `data: `[Bundle](https://developer.android.com/reference/android/os/Bundle.html)          | matches[Credential.data](https://developer.android.com/reference/kotlin/androidx/credentials/Credential#data()), the credential data in the[Bundle](https://developer.android.com/reference/android/os/Bundle.html)format; this should be constructed and retrieved from the a given[Credential](https://developer.android.com/reference/kotlin/androidx/credentials/Credential)itself and never be created from scratch |

## Public properties

### data

Added in[1.2.0](https://developer.android.com/jetpack/androidx/releases/credentials#1.2.0)  

```
val data: Bundle
```

the credential data in the[Bundle](https://developer.android.com/reference/android/os/Bundle.html)format  

### type

Added in[1.2.0](https://developer.android.com/jetpack/androidx/releases/credentials#1.2.0)  

```
val type: String
```

the credential type determined by the credential-type-specific subclass (e.g.[PasswordCredential.TYPE_PASSWORD_CREDENTIAL](https://developer.android.com/reference/kotlin/androidx/credentials/PasswordCredential#TYPE_PASSWORD_CREDENTIAL())for`PasswordCredential`or[PublicKeyCredential.TYPE_PUBLIC_KEY_CREDENTIAL](https://developer.android.com/reference/kotlin/androidx/credentials/PublicKeyCredential#TYPE_PUBLIC_KEY_CREDENTIAL())for`PublicKeyCredential`)