# Use secrets in Databricks

Below scripts base on python notebook

```
dbutils.secrets.help()
```

> &#x20;Provides utilities for leveraging secrets within notebooks. Databricks documentation for more info.
>
> **get(scope: String, key: String): String** -> Gets the string representation of a secret value with scope and key\
> **getBytes(scope: String, key: String): byte\[]** -> Gets the bytes representation of a secret value with scope and key\
> **list(scope: String): Seq** -> Lists secret metadata for secrets within a scope\
> **listScopes: Seq** -> Lists secret scopes

```
dbutils.secrets.listScopes()
```

> Out\[2]: \[SecretScope(name='ADBDataLakeKeyVault'), SecretScope(name='DataLakeKeyVault')]

```
dbutils.secrets.list(scope="ADBDataLakeKeyVault")
```

> Out\[7]: \[SecretMetadata(key='ADSL-AccountKey'),
>
> SecretMetadata(key='ADSL-AccountName'),
>
> SecretMetadata(key='ADSL-ContainerName-DWData'),&#x20;
>
> SecretMetadata(key='ADSL-ContainerName-ODSData'),&#x20;
>
> SecretMetadata(key='ADSL-ContainerName-RawData')]

```
dbutils.secrets.list(scope="DataLakeKeyVault")
```

> Out\[8]: \[SecretMetadata(key='ADSL-AccountKey'),&#x20;
>
> SecretMetadata(key='ADSL-AccountName'),&#x20;
>
> SecretMetadata(key='ADSL-ContainerName-DWData'),&#x20;
>
> SecretMetadata(key='ADSL-ContainerName-ODSData'),&#x20;
>
> SecretMetadata(key='ADSL-ContainerName-RawData')]

```
dbutils.secrets.get(scope="ADBDataLakeKeyVault", key="ADSL-AccountKey")
```

> Out\[11]: '\[REDACTED]'

```
spark.conf.set(
  "fs.azure.account.key." + dbutils.secrets.get(scope="DataLakeKeyVault",key="ADSL-AccountName") + ".dfs.core.windows.net",
  dbutils.secrets.get(scope="DataLakeKeyVault",key="ADSL-AccountKey"))
```

```
filePath = "abfss://" + dbutils.secrets.get(scope="DataLakeKeyVault",key="ADSL-ContainerName-RawData") + "@" + dbutils.secrets.get(scope="DataLakeKeyVault",key="ADSL-AccountName") + ".dfs.core.windows.net/"
```

```
dbutils.fs.ls(filePath)
```

> Out\[14]: \[FileInfo(path='abfss\://\[REDACTED]@\[REDACTED].dfs.core.windows.net/Customer.csv', name='Customer.csv', size=196514)]

```
dfCustomer = spark.read.format("csv") \
              .options(header='true', inferSchema='true') \
              .load(filePath + "Customer.csv")
```

```
dfCustomer.createOrReplaceTempView("customer")
```

```
%sql
select * from customer
```

## Reference

[Secrets CLI](https://docs.microsoft.com/en-us/azure/databricks/dev-tools/cli/secrets-cli)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://greece760.gitbook.io/azure-databricks-learning/environment-configuration/use-secrets-in-databricks.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
