# JavaScript Example for inference API

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fjavascript-example-for-inference-api%2F5975%3E%5C
**Category:** Beginners\
**Created:** [May 4, 2021, 11:07am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fjavascript-example-for-inference-api%2F5975 "2021-05-04T11:07:32Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![hgarg](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/hgarg/32/1592_2.png) [@hgarg](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fhgarg)%5C
**Post date:** [May 4, 2021, 11:07am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fjavascript-example-for-inference-api%2F5975%2F1 "2021-05-04T11:07:32Z")

</div>

Hi,

Is there a JavaScript example for using inference API - [🤗 Accelerated Inference API — Api inference documentation](/static-proxy?url=https%3A%2F%2Fapi-inference.huggingface.co%2Fdocs%2Fpython%2Fhtml%2Findex.html)

---

<div class="post-metadata">

**Author:** ![Narsil](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/narsil/32/40029_2.png) [@Narsil](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FNarsil)%5C
**Post date:** [May 4, 2021, 3:31pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fjavascript-example-for-inference-api%2F5975%2F2 "2021-05-04T15:31:15Z")

</div>

Hi @hgarg ,

Currently we don’t provide documentation for JS (or TS).

You could probably use simple `fetch`:

```javascript
const HF_API_TOKEN = "api_xxxx";
const model = "XXX"

const data = {inputs:"Something here"};

const response = await fetch(`/static-proxy?url=https%3A%2F%2Fapi-inference.huggingface.co%2Fmodels%2F%24%7Bmodel%7D%60%2C {headers: {"Authorization": `Bearer ${HF_API_TOKEN}`}, method: "POST", data:JSON.stringify(data)});
const data = await response.json()

```

should work.  
Cheers,  
Nicolas

---

<div class="post-metadata">

**Author:** ![hgarg](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/hgarg/32/1592_2.png) [@hgarg](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fhgarg)%5C
**Post date:** [May 4, 2021, 4:45pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fjavascript-example-for-inference-api%2F5975%2F3 "2021-05-04T16:45:34Z")

</div>

thanks @Narsil

---

<div class="post-metadata">

**Author:** ![hgarg](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/hgarg/32/1592_2.png) [@hgarg](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fhgarg)%5C
**Post date:** [May 5, 2021, 5:07am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fjavascript-example-for-inference-api%2F5975%2F4 "2021-05-05T05:07:22Z")

</div>

## I am facing these weird problems while calling the API using JavaScript. Example… Model - EleutherAI/gpt-neo-2.7B Input - { inputs: 'cat jumps over the mouse ’ } Output - [{ generated\_text: “import React from ‘react’;\n” + “import { get } from ‘lodash’;\n” + “import { getText, getLanguage } from ‘../../../../../utils/utils’;\n” + “import { Component } from '../../../components” }]

## Model - facebook/bart-large-cnn input - { inputs: ‘The tower is 324 metres (1,063 ft) tall, about the same height as an 81-storey building, and the tallest structure in Paris. Its base is square, measuring 125 metres (410 ft) on each side. During its construction, the Eiffel Tower surpassed the Washington Monument to become the tallest man-made structure in the world, a title it held for 41 years until the Chrysler Building in New York City was finished in 1930. It was the first structure to reach a height of 300 metres. Due to the addition of a broadcasting aerial at the top of the tower in 1957, it is now taller than the Chrysler Building by 5.2 metres (17 ft). Excluding transmitters, the Eiffel Tower is the second tallest free-standing structure in France after the Millau Viaduct.’ } output - [{ summary\_text: ‘[CNN.com](http://CNN.com) will feature [iReporter.com](http://iReporter.com) in a iReporters gallery next week. Please send photos of your pets to [iReport.com](http://iReport.com). i-Reporters. [i-Photography.com](http://i-Photography.com) is a team of journalists at the U.S. Embassy in Washington, DC.’ } ]

This happens every time, no matter the input. The same thing works fine when calling the API from python.

---

<div class="post-metadata">

**Author:** ![mandible](https://avatars.discourse-cdn.com/v4/letter/m/53a042/32.png) [@mandible](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fmandible)%5C
**Post date:** [May 7, 2021, 3:52am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fjavascript-example-for-inference-api%2F5975%2F5 "2021-05-07T03:52:22Z")

</div>

Same here! I get the “[CNN.com](http://CNN.com) will feature…” response for any input. Same outcome using `sshleifer/distilbart-cnn-12-6` too. Only JS, not Py.

---

<div class="post-metadata">

**Author:** ![Narsil](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/narsil/32/40029_2.png) [@Narsil](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FNarsil)%5C
**Post date:** [May 7, 2021, 11:05am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fjavascript-example-for-inference-api%2F5975%2F6 "2021-05-07T11:05:42Z")

</div>

Hi, we added NodeJS to the documentation so you can check out working examples:

> **[Overview](https://huggingface.co/docs/api-inference/quicktour)**
>
> We’re on a journey to advance and democratize artificial intelligence through open source and open science.

Does that work better ? I think my example had an error.

---

<div class="post-metadata">

**Author:** ![mandible](https://avatars.discourse-cdn.com/v4/letter/m/53a042/32.png) [@mandible](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fmandible)%5C
**Post date:** [May 7, 2021, 4:05pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fjavascript-example-for-inference-api%2F5975%2F8 "2021-05-07T16:05:59Z")

</div>

Yes, this returns the correct summary! Thanks for your help. Is there any way to specify the min\_length of the summary with the API? [Detailed parameters — Api inference documentation](/static-proxy?url=https%3A%2F%2Fapi-inference.huggingface.co%2Fdocs%2Fnode%2Fhtml%2Fdetailed_parameters.html%23id6) appears there’s just three unrelated options.

---

<div class="post-metadata">

**Author:** ![NarsilTest](https://avatars.discourse-cdn.com/v4/letter/n/e480ec/32.png) [@NarsilTest](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FNarsilTest)%5C
**Post date:** [May 7, 2021, 4:28pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fjavascript-example-for-inference-api%2F5975%2F9 "2021-05-07T16:28:27Z")

</div>

You can use it.

It’s an unofficial parameter because it’s defined in `tokens` which are hard to use properly when you manipulate string. But before we have string oriented parameters you can use this one !

Cheers,  
Nicolas
