---
sourceDocument: Xanadu API Reference
sourceDocumentLink: https://servicenow-prod.fluidtopics.net/r/xanadu/api-reference

 Release :

    - xanadu

ft:locale :

    - en-US

ft:publication_title :

    - Xanadu API Reference

ft:clusterId :

    - crapiref

bundleId :

    - crapiref

workflow :

    - Creator


---

# NowAPIService class - iOS

# NowAPIService class - iOS {#ariaid-title1}

* Release version: Xanadu
* 
* Updated August 1, 2024
* 
* ![](https://www.servicenow.com/docs/portal-asset/ico-clock) 6 minutes to read

The NowAPIService class provides functions that enable you to
perform requests to a specified ServiceNow REST API.
{#NowAPIServiceiOSAPI__table_vx2_klw_5pb__entry__3}

| Name | Type | Description |
|-|-|-|
| configuration | [NowServiceConfiguration](https://servicenow-prod.fluidtopics.net/A6W2zQ0fOO_VQ9lbo3VKwQ#NowServiceConfigurationiOSStruct "The NowServiceConfiguration structure defines configuration information for a feature service.") | Service configuration to associate with the protocol. |
[Table 1. Properties]

{#NowAPIServiceiOSAPI__table_vx2_klw_5pb}

## NowAPIService - data(for endpoint: NowAPIEndpoint, queryItems: \[String: String\],
httpHeaders: \[String: String\], body: Data, completion: @escaping (Result\<DataResponse,
NowDataError\>) {#ariaid-title2}

Requests data from the specified ServiceNow REST API and then
executes the completion handler.
{#NAPIiOS-data_O_A_A_S_O__table_jck_kyp_5pb__entry__3}{#NAPIiOS-data_O_A_A_S_O__mobilesdkiOS-completion_data_dataerror-row}

| Name | Type | Description |
|-|-|-|
| for endpoint | [NowAPIEndpoint](https://servicenow-prod.fluidtopics.net/O4BvX5DygYfVkeV135htVw#NowAPIEndpointiOSProtocol "The NowAPIEndpoint protocol provides an interface for accessing any publicly exposed ServiceNow REST APIs.") | Object containing the ServiceNow REST API endpoint to access, including the HTTP method, relative path to the endpoint, path parameters, and whether authentication is required. |
| queryItems | Array | Optional. Query parameters for the endpoint as key/value pairs. Default: nil |
| httpHeaders | Array | Optional. Request HTTP headers required by the endpoint as key/value pairs. Default: nil |
| body | String | Optional. Request body parameters. Default: nil |
| completion | @escaping (Result\<DataResponse, NowDataError\>) | Completion handler to execute after the REST API call completes. Return values for the completion handler: * Success: DataResponse that contains the returned data from the REST API call. * Failure: NowDataError * `accessToken(AccessTokenProviderError)` * AccessTokenProviderError: The access token provider's error code or message. * accessTokenRetrievalFailed * userSessionError(_ error: Error) Thrown when there is an error in the access token. * `attachmentValidation` Thrown when an attachment fails validation. * `badResponse(statusCode: HTTPStatusCode)` * HTTPStatusCode: Status code received from the instance. Thrown when a request returns an unexpected response * `cannotDecodeModel(DecodingError)` * DecodingError: Decoding error detected. Thrown when a Codable model cannot be decoded from JSON. * `cannotDecodeProperty(type: Any, from: String)` * type: Wrapped type to decode from a string. * from: String to decode to the specified type. Thrown when a string-wrapped value cannot be decoded from JSON. * `cannotEncodeModel(EncodingError)` * EncodingError: Encoding error detected. Thrown when a Codable model cannot be encoded to JSON. * `cannotParseResponse` Thrown when a response from the instance cannot be parsed into its expected format. * `invalidURL` Thrown when a URL cannot be formed. For example, if the string contains characters that are illegal in a URL or is an empty string. * `missingAttachmentMetadata` Thrown when the attachment metadata header is missing. * `missingServiceConfiguration` Thrown when an expected service configuration is missing. * `missingSysID` Thrown when an expected sys_id parameter is missing. * `network(NetworkServiceError)` * genericError(String) * operationCanceled * serviceDisabled * serverError(Error) * systemError(Error) Thrown when a network service encountered an error. {#NAPIiOS-data_O_A_A_S_O__NowDataErroriOS-ul-enum} {#NAPIiOS-data_O_A_A_S_O__ul_uzx_thj_ppb} |
[Table 2. Parameters]

{#NAPIiOS-data_O_A_A_S_O__table_jck_kyp_5pb} {#NAPIiOS-data_O_A_A_S_O__table_kck_kyp_5pb__entry__2}

| Type | Description |
|-|-|
| None |   |
[Table 3. Returns]

{#NAPIiOS-data_O_A_A_S_O__table_kck_kyp_5pb}  
The following code example shows how to call this function.

    struct Endpoint: NowAPIEndpoint {
      let httpMethod: HTTPMethod
      let relativePath: String
      let requiresAuthentication: Bool
    }

    /// HTTP request method
    public enum HTTPMethod: CaseIterable {
        case options, get, head, post, put, patch, delete, trace, connect
    }
    let relativePath = "/api/now/account"
    let endPoint = Endpoint(httpMethod: HTTPMethod.get, relativePath: relativePath, requiresAuthentication: true)

    apiService.data(for: endPoint, queryItems: ["name": "abel"], httpHeaders: httpHeaders, body: body.data(using: .utf8)) { [weak self] (result) in
      switch result {
        case .success(let response):
        // Data request successful
        case .failure(let error):
        // Data request failed, return NowDataError
        // .failure(error)
      }
    }

## NowAPIService - data(for endpoint: NowAPIEndpoint, queryItems: \[String: String\], httpHeaders: \[String: String\], body: Data) async throws {#ariaid-title3}

Requests data from the specified ServiceNow REST API.
{#NAPIiOS-data-async_O_A_A_S_O__table_jck_kyp_5pb__entry__3}

| Name | Type | Description |
|-|-|-|
| for endpoint | [NowAPIEndpoint](https://servicenow-prod.fluidtopics.net/O4BvX5DygYfVkeV135htVw#NowAPIEndpointiOSProtocol "The NowAPIEndpoint protocol provides an interface for accessing any publicly exposed ServiceNow REST APIs.") | Object containing the ServiceNow REST API endpoint to access, including the HTTP method, relative path to the endpoint, path parameters, and whether authentication is required. |
| queryItems | Array | Optional. Query parameters for the endpoint as key/value pairs. Default: nil |
| httpHeaders | Array | Optional. Request HTTP headers required by the endpoint as key/value pairs. Default: nil |
| body | String | Optional. Request body parameters. Default: nil |
[Table 4. Parameters]

{#NAPIiOS-data-async_O_A_A_S_O__table_jck_kyp_5pb} {#NAPIiOS-data-async_O_A_A_S_O__table_kck_kyp_5pb__entry__2}

| Type | Description |
|-|-|
| DataResponse | Returned when the method is successful. Data from the REST API call. |
| NowDataError | Thrown when the method fails. * `accessToken(AccessTokenProviderError)` * AccessTokenProviderError: The access token provider's error code or message. * accessTokenRetrievalFailed * userSessionError(_ error: Error) Thrown when there is an error in the access token. * `attachmentValidation` Thrown when an attachment fails validation. * `badResponse(statusCode: HTTPStatusCode)` * HTTPStatusCode: Status code received from the instance. Thrown when a request returns an unexpected response * `cannotDecodeModel(DecodingError)` * DecodingError: Decoding error detected. Thrown when a Codable model cannot be decoded from JSON. * `cannotDecodeProperty(type: Any, from: String)` * type: Wrapped type to decode from a string. * from: String to decode to the specified type. Thrown when a string-wrapped value cannot be decoded from JSON. * `cannotEncodeModel(EncodingError)` * EncodingError: Encoding error detected. Thrown when a Codable model cannot be encoded to JSON. * `cannotParseResponse` Thrown when a response from the instance cannot be parsed into its expected format. * `invalidURL` Thrown when a URL cannot be formed. For example, if the string contains characters that are illegal in a URL or is an empty string. * `missingAttachmentMetadata` Thrown when the attachment metadata header is missing. * `missingServiceConfiguration` Thrown when an expected service configuration is missing. * `missingSysID` Thrown when an expected sys_id parameter is missing. * `network(NetworkServiceError)` * genericError(String) * operationCanceled * serviceDisabled * serverError(Error) * systemError(Error) Thrown when a network service encountered an error. {#NAPIiOS-data-async_O_A_A_S_O__NowDataErroriOS-ul-enum} |
[Table 5. Returns]

{#NAPIiOS-data-async_O_A_A_S_O__table_kck_kyp_5pb}  
The following code examples shows how to call this method.

    do {
        let dataResponse = try await apiService.data(for: endpoint, queryItems: queryItems, httpHeaders: httpHeaders, body: body)
        // Data request successful
    } catch {
        // Data request failed, NowDataError thrown
    }

## NowAPIService - data(for endpoint: NowAPIEndpoint, queryItems: \[String: String\]? = nil, httpHeaders: \[String: String\]? = nil, body: Data? = nil) {#ariaid-title4}

Requests data from the specified ServiceNow REST API.
Note:  
This method has been deprecated. You should use the async/await implementation of the method instead.
{#NAPIiOS-data_O_A_A_S__table_sfb_5tp_5pb__entry__3}

| Name | Type | Description |
|-|-|-|
| for endpoint | [NowAPIEndpoint](https://servicenow-prod.fluidtopics.net/O4BvX5DygYfVkeV135htVw#NowAPIEndpointiOSProtocol "The NowAPIEndpoint protocol provides an interface for accessing any publicly exposed ServiceNow REST APIs.") | Object containing the ServiceNow REST API endpoint to access, including the HTTP method, relative path to the endpoint, path parameters, and whether authentication is required. |
| queryItems | Array | Optional. Query parameters for the endpoint as key/value pairs. Default: nil |
| httpHeaders | Array | Optional. Request HTTP headers required by the endpoint as key/value pairs. Default: nil |
| body | Data | Optional. Request body parameters. Default: nil |
[Table 6. Parameters]

{#NAPIiOS-data_O_A_A_S__table_sfb_5tp_5pb} {#NAPIiOS-data_O_A_A_S__table_tfb_5tp_5pb__entry__2}

| Type | Description |
|-|-|
| AnyPublisher\<DataResponse, NowDataError\> | Success: Data returned by the REST API. Failure: NowDataError * `accessToken(AccessTokenProviderError)` * AccessTokenProviderError: The access token provider's error code or message. * accessTokenRetrievalFailed * userSessionError(_ error: Error) Thrown when there is an error in the access token. * `attachmentValidation` Thrown when an attachment fails validation. * `badResponse(statusCode: HTTPStatusCode)` * HTTPStatusCode: Status code received from the instance. Thrown when a request returns an unexpected response * `cannotDecodeModel(DecodingError)` * DecodingError: Decoding error detected. Thrown when a Codable model cannot be decoded from JSON. * `cannotDecodeProperty(type: Any, from: String)` * type: Wrapped type to decode from a string. * from: String to decode to the specified type. Thrown when a string-wrapped value cannot be decoded from JSON. * `cannotEncodeModel(EncodingError)` * EncodingError: Encoding error detected. Thrown when a Codable model cannot be encoded to JSON. * `cannotParseResponse` Thrown when a response from the instance cannot be parsed into its expected format. * `invalidURL` Thrown when a URL cannot be formed. For example, if the string contains characters that are illegal in a URL or is an empty string. * `missingAttachmentMetadata` Thrown when the attachment metadata header is missing. * `missingServiceConfiguration` Thrown when an expected service configuration is missing. * `missingSysID` Thrown when an expected sys_id parameter is missing. * `network(NetworkServiceError)` * genericError(String) * operationCanceled * serviceDisabled * serverError(Error) * systemError(Error) Thrown when a network service encountered an error. {#NAPIiOS-data_O_A_A_S__NowDataErroriOS-ul-enum} |
[Table 7. Returns]

{#NAPIiOS-data_O_A_A_S__table_tfb_5tp_5pb}  
The following code example shows how to call this function.

    struct Endpoint: NowAPIEndpoint {
        let httpMethod: HTTPMethod
        let relativePath: String
        let requiresAuthentication: Bool
    }

    /// HTTP request method
    public enum HTTPMethod: CaseIterable {
        case options, get, head, post, put, patch, delete, trace, connect
    }
    let relativePath = "/api/now/account"
    let endPoint = Endpoint(httpMethod: HTTPMethod.get, relativePath: relativePath, requiresAuthentication: true)

    // Using Combine
    apiService.data(for: Endpoint(httpMethod: httpMethod, relativePath: endpointPath, requiresAuthentication: requiresAuthentication), queryItems: queryItems, httpHeaders: httpHeaders, body: body.data(using: .utf8))
      .receive(on: DispatchQueue.main)
      .sink { [weak self] (comp) in
        if case let .failure(error) = comp {
          // Data request failed, return NowDataError
        }
      } receiveValue: { [weak self] (response) in
        // Data request successful
        self?.publish(data: response.data)
      }
      .store(in: &self.subscriptions)
    }


