0

I am trying to delete a customer from RevenueCat if the user decides to delete their account. This is the code I implemented:

private func deleteRevenueCatAccount(forUserID: String) {
    
    let headers = ["accept": "application/json", "Authorization": "Bearer \(secretApiKey)"]
    let baseURL = "https://api.revenuecat.com/v1"
    let deleteEndpoint = "/subscribers/\(forUserID)"
    let urlString = baseURL + deleteEndpoint
    
    guard let url = URL(string: urlString) else {
        // Handle invalid URL error
        return
    }
    
    // Create the request
    let request = NSMutableURLRequest(
        url: url,
        cachePolicy: .useProtocolCachePolicy,
        timeoutInterval: 10.0
    )
    
    request.httpMethod = "DELETE"
    request.allHTTPHeaderFields = headers
    
    // Create the session
    let session = URLSession.shared
    
    // Send the request
    let task = session.dataTask(with: request as URLRequest) { (data, response, error) in
        
        if let error = error {
            // Handle error
            print("Error: \(error.localizedDescription)")
            return
        }
        
        // Check the response
        if let httpResponse = response as? HTTPURLResponse {
            
            if httpResponse.statusCode == 204 {
                // Customer deleted successfully
                print("Customer deleted successfully")
            } else {
                // Handle non-successful status code
                print("Error: \(httpResponse.statusCode)")
            }
            
        } //: HTTP RESPONSE
        
    } //: DATA TASK
    
    task.resume()
    
} //: FUNC

However, it throws an error with a status code 400 in the HTTPURLResponse.

Any ideas what could be the problem?

**Note:

The user that I am trying to delete is a sandbox user since I am still in development, but after contacting the RevenueCat support, they told me that it should still work for sandbox users.

  • Unrelated but `NSMutableURLRequest` should just be `URLRequest`. – Larme May 25 '23 at 08:08
  • This seems like a question that needs to be answered by RevenueCat, who understands what the request is supposed to look like. My guess is that something is wrong in your JSON blob or perhaps with the request method, but without knowing their API and without an example of the JSON blob that you're sending, that's a total guess. – dgatwood Jun 09 '23 at 17:20

1 Answers1

1

I work at RevenueCat,

If I had to guess, without looking at the response body of the request, it might be that you didn't explicitly state Content-Type: application/json in the request headers, try adding it:

 let headers = ["accept": "application/json", "Content-Type": "application/json", "Authorization": "Bearer \(secretApiKey)"]

However you shouldn't use a Secret API key inside your App, these are meant to be securely used in your server, otherwise bad actors could perform destructive actions on your account!

Alfonso Embid-Desmet
  • 3,561
  • 3
  • 32
  • 45