# OpenApi

## OpenAPI Company Data

Retrieve company information using VAT codes through the OpenAPI.it interface. This business function allows you to fetch detailed company data from the OpenAPI.it service for EU companies.

### Settings Configuration

Before using the OpenAPI Company Data function, configure the following settings in your workspace:

#### OpenAPI Username

* **Setting**: `OpenApiUsername`
* **Description**: Your OpenAPI.it username for authentication
* **Required**: Yes

#### OpenAPI Password

* **Setting**: `OpenApiPassword`
* **Description**: Your OpenAPI.it token for authentication
* **Required**: Yes

#### Test Environment

* **Setting**: `OpenApiTest`
* **Description**: Enable to use test environment URLs instead of production
* **Default**: false
* **Required**: No

When enabled, the function uses:

* Test OAuth URL: `https://test.oauth.openapi.it`
* Test Company URL: `https://test.company.openapi.com`

When disabled (production), the function uses:

* Production OAuth URL: `https://oauth.openapi.it`
* Production Company URL: `https://company.openapi.com`

### Response Format

#### Successful Response

```json
{
  "status": 200,
  "message": "Company data retrieved successfully",
  "data": {
    "vatCode": "12485671007",
    "companyName": "OPENAPI S.P.A.",
    "valid": true,
    "formatValid": true,
    "countryCode": "IT",
    "companyAddress": "VIALE F TOMMASO MARINETTI 221 00143 ROMA RM"
  }
}
```

#### Error Responses

**Missing VAT Code**

```json
{
  "status": 400,
  "message": "Missing vatCode parameter"
}
```

**Invalid Settings**

```json
{
  "status": 400,
  "message": "Invalid settings. Check configuration"
}
```

**Authentication Failed**

```json
{
  "status": 401,
  "message": "Authentication failed",
  "error": "Authentication error details"
}
```

**Company Not Found**

```json
{
  "status": 404,
  "message": "No company data found"
}
```

**Server Error**

```json
{
  "status": 500,
  "message": "Error processing VAT code",
  "error": "Error details"
}
```

### Example Usages

#### Basic Company Data Retrieval

```javascript
// Request company data for a company
let result = app.businessFunction({
  functionName: "OpenApi",
  methodName: "GetCompanyData",
  vatCode: "12485671007"
});

app.setFieldValue('txtResult', JSON.stringify(result, null,2));

/* Result:
{
  "status": 200,
  "message": "Company data retrieved successfully",
  "data": {
    "vatCode": "12485671007",
    "companyName": "OPENAPI S.P.A.",
    "valid": true,
    "formatValid": true,
    "countryCode": "IT",
    "companyAddress": "VIALE F TOMMASO MARINETTI 221 00143 ROMA RM"
  }
}
*/
```

