# Basic Math API Documentation (#Experimental)

## :rocket: Quickstart Using cURL

```bash
curl -X POST "https://infinitode.netlify.app/api/v1/basic-math" \
     -H "Content-Type: application/json" \
     -d '{"apiKey": "YOUR_API_TOKEN", "expression": "2+2"}'
```

## Changelog

* **1.1 (Latest)**: Updated API security.
* **1.0**: Initial commit for API space. Marked as experimental.

{% hint style="info" %}
You can view more information on why this API was marked as experimental here:

[#why-is-this-api-experimental](#why-is-this-api-experimental "mention")
{% endhint %}

## Access

The API can be accessed from

```html
https://infinitode.netlify.app/api/v1/basic-math
```

***

## Authentication

### Bearer

This API uses Bearer Token authentication. Include your API token in the `Authorization` header of your request.

```
Authorization: Bearer YOUR_API_TOKEN
```

{% hint style="info" %}
You can also use the API with the default `GET` method, and continue to use the default `apiKey` or `token` parameters. When using the authorization bearer, `apiKey` or `token` parameters are automatically ignored.
{% endhint %}

### API Token

To use this API, you must include an API token in the request. The API token should be included in the request body as the `apiKey` field.

```json
{
  "apiKey": "YOUR_API_TOKEN"
}
```

You can also use the API with the default `GET` method.

{% code overflow="wrap" %}

```url
https://infinitode.netlify.app/api/v1/basic-math?token=YOUR_API_TOKEN
```

{% endcode %}

***

## Endpoint

Endpoint URL:

```
https://infinitode.netlify.app/api/v1/basic-math
```

HTTP Methods:

* `Post`
* `Get`

Request Body

The request body should be a JSON object with the following properties:

* `apiKey`: Your API token.
* `expression`: The basic math expression, as a string. For supported operations, see the table below.

Example:

```json
{
  "apiKey": "YOUR_API_TOKEN",
  "expression": "1+1"
}
```

Or, use the `GET` method:

{% code overflow="wrap" %}

```url
https://infinitode.netlify.app/api/v1/basic-math?token=YOUR_API_TOKEN&expression=1+1
```

{% endcode %}

Response

Upon a successful request, you will receive a JSON response with the solved expression. If the API token is invalid or the request fails for any reason, an error message will be included in the response. For developer API calls, validation results will also be included along with **dummy content**.

Example response:

```json
{
  "expressionResult": 2
}
```

The response includes the expression's result and validation results for the expression input are excluded, since this was a normal API call.

***

## Usage example

Here's a usage example in JavaScript using the `fetch` API:

```javascript
const apiUrl = 'https://infinitode.netlify.app/api/v1/basic-math';

const data = {
  "apiKey": "YOUR_API_TOKEN",
  "expression": "1+1"
};

fetch(apiUrl, {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
  },
  body: JSON.stringify(data),
})
  .then(response => response.json())
  .then(result => {
    console.log('Expression Result:', result.expressionResult);
  })
  .catch(error => {
    console.error('Error:', error);
  });
```

Make sure to replace `'YOUR_API_TOKEN'` with your actual API token.

***

## Error handling

The API may return error responses in case of invalid input or other issues. Be sure to handle errors gracefully in your application. Error responses will include appropriate status codes and error messages in the JSON format.

***

## Parameters

Here are all the available parameters for the API:

#### \`POST\` method

| Parameter    | Description                                                        | Optional |
| ------------ | ------------------------------------------------------------------ | -------- |
| `apiKey`     | Your API key, either the developer API key, or your normal API key | `false`  |
| `expression` | The expression (string) to be evaluated.                           | `false`  |

#### \`GET\` method

| Parameter    | Description                                                        | Optional |
| ------------ | ------------------------------------------------------------------ | -------- |
| `token`      | Your API key, either the developer API key, or your normal API key | `false`  |
| `expression` | The expression (string) to be evaluated.                           | `false`  |

#### Valid Operation Types

* `+` Addition
* `-` Subtraction
* `*` Multiplication
* `/` Division
* `^` Powers
* `log` Logarithmic Functions
* `sin` Sine Functions
* `cos` Cosine Functions
* `tan` Tangent Functions

***

## :warning: Error codes

Here are a few of the error codes you will receive in responses to help clarify issues:

#### Common error codes

<table><thead><tr><th width="177">Error code</th><th>Description</th></tr></thead><tbody><tr><td><code>400</code></td><td>Missing or invalid parameters. This is when the parameters you passed to the API, do not match the API's expected inputs.</td></tr><tr><td><code>405</code></td><td>Using an invalid method, to access the API.</td></tr><tr><td><code>429</code></td><td>The rate limit is exceeded within the current timeframe.</td></tr><tr><td><code>401</code></td><td>Error during the request. This can be because of insufficient API calls, input errors, or errors arising from the API's logic.</td></tr></tbody></table>

{% hint style="info" %}
Our APIs are rigorously tested before deployments to ensure that they do not produce any unintentional errors during production.
{% endhint %}

***

## :hourglass: Rate limiting

We use rate limiting to protect our API's resources. Rate limiting is implemented on all of our APIs and follows these general principles:

\
\- `interval`: The rate limiting is set to a default interval of `1 minute`.\
\- `amount of requests`: The amount of requests is set to a default of `25 requests` within the given interval.

If the rate limit is exceeded, an error `429` code will be returned, instead of the actual API output.

***

## :star: Best practices

Common guidelines to use, optimize, and protect your implementation of our APIs.

* Do not store/share/expose your API keys
* Implement data cleaning and validation in your code to ensure inputs are sanitized
* Handle API errors gracefully
* Use Developer API calls to help debug your implementation and code
* Handle API outputs accordingly. You can see examples of this API's output in the [#endpoint](#endpoint "mention") section.
* Implement code to notify users of actions when using our APIs, such as errors, delays, formatting issues, etc.

***

## :question: FAQs

Answers to the most common questions and issues when using this API:

<details>

<summary>I am getting a <code>405</code> error, what should I do?</summary>

When you are getting a `404` or `NotFound` error, you likely misspelled the API endpoint or URL. This could also be caused by using an invalid method to access the API.

</details>

<details>

<summary>Is rate limiting set individually, or per API key?</summary>

Rate limiting is set `individually`, this means that rate limiting happens on an end-user level. This ensures that our API is used fairly, among all users.

</details>

<details>

<summary>Are request timeouts a thing?</summary>

Yes, we normally timeout requests after the 10-second mark. This, however, can vary significantly, depending on various other factors.

</details>

<details>

<summary>What is the max payload size?</summary>

The maximum payload size for `buffered executions` is `4.5mb` per request. For `streamed executions`, we use a payload restriction size of `20mb` per request.

</details>

<details>

<summary>What is the recommended way to call the API?</summary>

If you can, call the API using the `POST` method. The `GET` method works but is not recommended, especially when you're calling it by directly modifying the URL. This is because URL parameters that are strings, can sometimes be misformatted afterwards, and then incorrectly passed to the API.

</details>

<details>

<summary>Why is this API experimental?</summary>

This API did not pass all of our tests during our testing phase. This means that this API still works but may produce outputs that aren't accurate. Use with caution.

</details>

<details>

<summary>Why is my expression not evaluated correctly?</summary>

This API is `experimental`, meaning that it did not pass all of our tests, in terms of accuracy. It evaluates basic math expressions correctly, but as soon as the expression gets a bit more complex, the API does not evaluate it correctly.

</details>

<details>

<summary>Future fixes, and next steps?</summary>

We are planning on introducing a bunch of new APIs, and updates to our older APIs, which includes this one. You can view our [blog](https://infinitode.netlify.app/blog) for more details about when that will happen.

</details>

***

## OpenAPI

You can download the OpenAPI specification for this API to learn more about how it works and how to use it. The OpenAPI specification is a standard, language-agnostic interface to RESTful APIs that allows both humans and computers to discover and understand the capabilities of the service without access to source code, documentation, or through network traffic inspection.

[Download OpenAPI Specification](https://infinitode.netlify.app/openapi/basic-math/openapi.yaml)

***

## Conclusion

That's it! You now have the basics to use the Basic Math API. If you have any further questions or encounter issues, please contact our support team.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://infinitode-docs.gitbook.io/documentation/api-documentation/basic-math-api-documentation-experimental.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
