title: How to pass context inside Retrofit interface
body: <p>I want to pass header authorization token in HTTP requests in retrofit. The token is saved in DataStore. My problem is how to retrieve the token from DataStore and pass it to the intercepter header. I've spent hours thinking of a workaround and searching through the internet but I came with nothing. I'm also new to Kotlin. Here is the code snippet:</p>
  <pre><code>interface RoomAPIService {
  
  @GET(&quot;rooms&quot;)
  fun getAllRooms(@Header(&quot;Authorization&quot;)  authHeader: String): Call&lt;List&lt;Room&gt;&gt;
  var context: Context
  
  
  companion object {
      var retrofitService: RoomAPIService? = null
      var token: String = &quot;&quot;
      fun getInstance() : RoomAPIService {
  
          GlobalScope.launch(Dispatchers.IO)
          {
              //How to pass context to DataRepository.getInstance(context)
            token  = DataStoreRepository.getInstance().getToken().toString()
          }
              val httpClient = OkHttpClient.Builder()
          httpClient.addInterceptor { chain -&gt;
              val request =           chain.request().newBuilder().addHeader(&quot;Authorization&quot;,&quot;Bearer &quot; +  theTokenRetrievedFromDataStore).build()
              chain.proceed(request)
          }
          .
          .
          }
       
          .
          .
           }
  </code></pre>
  <p>Here is <code>DataStoreRepository.kt</code>:</p>
  <pre><code>class DataStoreRepository(context: Context) {
  private val dataStore: DataStore&lt;Preferences&gt; = context.createDataStore(
      name = &quot;token_store&quot;
  )
  
  companion object {
  
      private val TOKEN = preferencesKey&lt;String&gt;(&quot;TOKEN&quot;)
      private var instance: DataStoreRepository? = null
  
      fun getInstance(context: Context): DataStoreRepository {
          return instance ?: synchronized(this) {
              instance ?: DataStoreRepository(context).also { instance = it }
          }
      }
  }
  
  suspend fun savetoDataStore(token: String) {
     
      dataStore.edit {
  
          it[TOKEN] = token
  
  
      }
  }
  
  suspend fun getToken(): String? {
      val preferences: Preferences = dataStore.data.first()
      Log.d(&quot;datastore&quot;, &quot;token retrieved: ${preferences[TOKEN]} +++++++++++&quot;)
      return preferences[TOKEN]
  }
  }
  </code></pre>
  <p>And here is MainActivity.kt:</p>
  <p>class MainActivity : AppCompatActivity() {</p>
  <pre><code> private lateinit var  logoutBtn: Button
   private lateinit var bottomNavigation: BottomNavigationView
   private lateinit var binding: ActivityMainBinding
   lateinit var viewModel: RoomViewModel
   private val retrofitService = RoomAPIService.getInstance(this)
   val adapter = RoomsAdapter()
   private var token: String = &quot;&quot;
  override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_main)
  
      binding = ActivityMainBinding.inflate(layoutInflater)
      setContentView(binding.root)
      logoutBtn = binding.logoutBtn
      bottomNavigation = binding.bottomNavigation
      viewModel = ViewModelProvider(this, RoomViewModeFactory(RoomRepository(retrofitService))).get(RoomViewModel::class.java)
      binding.recyclerview.adapter = adapter
  
  
          var token = intent.getStringExtra(&quot;token&quot;)
          Log.d(&quot;tokenCheck&quot;,&quot;checkToken: $token&quot;)
          if (token != null) {
              viewModel.getAllRooms(token)
          }
          ..
  </code></pre>
  <p>Any help will be greatly appreciated!</p>