Skip to content

Implement API key authentication

Prerequisites

Before starting this tutorial, you need the following:

  1. An ArcGIS Location Platform or ArcGIS Online account.

  2. A development and deployment environment that meets the system requirements.

  3. An IDE for Android development in Kotlin.

Steps

You need an access token to use the location services used in this tutorial.

  1. Go to the Create an API key tutorial to obtain a new API key access token.

    • Ensure that the following privilege is enabled: Location services > Basemaps > Basemap styles service.

    • Copy the access token as it will be used in the next step.

  2. In Android Studio, click the Android view in your app project. Then open app -> kotlin+java -> com.example.app -> MainActivity.kt.

    MainActivity.kt
    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    
    package com.example.app
    
    import android.os.Bundle
    import androidx.activity.ComponentActivity
    import androidx.activity.compose.setContent
    import androidx.activity.enableEdgeToEdge
    
    import com.arcgismaps.ApiKey
    import com.arcgismaps.ArcGISEnvironment
    
    import com.example.app.screens.MainScreen
    import com.example.app.ui.theme.TutorialTheme
    
    MainActivity.kt
    Expand
    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    class MainActivity : ComponentActivity() {
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
    
            ArcGISEnvironment.apiKey = ApiKey.create("YOUR_ACCESS_TOKEN")
    
            enableEdgeToEdge()
    
            setContent {
                TutorialTheme {
                    MainScreen()
                }
            }
        }
    }

For more information, see API key authentication.

Best Practice: The access token is stored directly in the code as a convenience for this tutorial. Do not store credentials directly in source code in a production environment.

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.