1# KWS SDK<a name="EN-US_TOPIC_0000001090776709"></a> 2 31. Add the API of the KWS SDK to the **//foundation/ai/engine /interfaces/kits** directory. This API can be called by third-party applications. The following code snippet is an example API for the KWS SDK. The reference code is available at the **//foundation/ai/engine /interfaces/kits/asr/keyword\_spotting** directory. 4 5 ``` 6 class KWSSdk { 7 public: 8 KWSSdk(); 9 virtual ~KWSSdk(); 10 11 // Create a KWS SDK instance. 12 int32_t Create(); 13 14 // Synchronously execute the KWS task. 15 int32_t SyncExecute(const Array<int16_t> &audioInput); 16 17 // Set the KWS callback. 18 int32_t SetCallback(const std::shared_ptr<KWSCallback> &callback); 19 20 // Destroy the KWS SDK instance to release the session engaged with the plug-in. 21 int32_t Destroy(); 22 }; 23 ``` 24 252. Add the API implementation of the SDK to the **//foundation/ai/engine/services/client/algorithm\_sdk** directory and call the APIs provided by the client to use the algorithm plug-in capabilities. The following code snippet is an example implementation of the **create** method in the API of the KWS SDK. For more details, see the reference code at **//foundation/ai/engine/services/client/algorithm\_sdk/asr/keyword\_spotting**. 26 27 ``` 28 int32_t KWSSdk::KWSSdkImpl::Create() 29 { 30 if (kwsHandle_ != INVALID_KWS_HANDLE) { 31 HILOGE("[KWSSdkImpl]The SDK has been created"); 32 return KWS_RETCODE_FAILURE; 33 } 34 if (InitComponents() != RETCODE_SUCCESS) { 35 HILOGE("[KWSSdkImpl]Fail to init sdk components"); 36 return KWS_RETCODE_FAILURE; 37 } 38 // Call the AieClientInit API provided by the client to initialize the engine service and activate IPC call. 39 int32_t retCode = AieClientInit(configInfo_, clientInfo_, algorithmInfo_, nullptr); 40 if (retCode != RETCODE_SUCCESS) { 41 HILOGE("[KWSSdkImpl]AieClientInit failed. Error code[%d]", retCode); 42 return KWS_RETCODE_FAILURE; 43 } 44 if (clientInfo_.clientId == INVALID_CLIENT_ID) { 45 HILOGE("[KWSSdkImpl]Fail to allocate client id"); 46 return KWS_RETCODE_FAILURE; 47 } 48 DataInfo inputInfo = { 49 .data = nullptr, 50 .length = 0, 51 }; 52 DataInfo outputInfo = { 53 .data = nullptr, 54 .length = 0, 55 }; 56 // Call the AieClientPrepare API provided by the client to load the algorithm plug-in. 57 retCode = AieClientPrepare(clientInfo_, algorithmInfo_, inputInfo, outputInfo, nullptr); 58 if (retCode != RETCODE_SUCCESS) { 59 HILOGE("[KWSSdkImpl]AieclientPrepare failed. Error code[%d]", retCode); 60 return KWS_RETCODE_FAILURE; 61 } 62 if (outputInfo.data == nullptr || outputInfo.length <= 0) { 63 HILOGE("[KWSSdkImpl]The data or length of output info is invalid"); 64 return KWS_RETCODE_FAILURE; 65 } 66 MallocPointerGuard<unsigned char> pointerGuard(outputInfo.data); 67 retCode = PluginHelper::UnSerializeHandle(outputInfo, kwsHandle_); 68 if (retCode != RETCODE_SUCCESS) { 69 HILOGE("[KWSSdkImpl]Get handle from inputInfo failed"); 70 return KWS_RETCODE_FAILURE; 71 } 72 return KWS_RETCODE_SUCCESS; 73 } 74 ``` 75 76 The preceding code is the specific API implementation. The **create** function in the API of the KWS SDK calls the open **AieClientInit** and **AieClientPrepare** APIs provided by the client to connect to the server and load the algorithm model. For details, see the implementation of the **create** method in following sections. 77 78 > **NOTE:** 79 >The sequence for the SDK to call client APIs: **AieClientInit** -\> **AieClientPrepare** -\> **AieClientSyncProcess** or **AieClientAsyncProcess** -\> **AieClientRelease** -\> **AieClientDestroy**. An exception will be thrown if the call sequence is violated. 80 81 82