Analyzing Code With Code Detection API

The world runs on code. Nearly every single aspect of our lives is in some way, shape, or form handled by code. It may be hidden and running behind the scenes, but it is ever-present in our daily activities. Waking in the morning thanks to our alarm clock or phone app, consuming news and media on a smart device or TV, ordering food for dine-in, take-out, or delivery, or even just navigating traffic to get from point A to point B—code is the driving force behind it all.

As such, the code that runs the world has grown exponentially, and it continues to do so daily as more apps and services aim to permeate our lives. Most of this code doesn’t exist on just one person’s computer. It is being written, shared, and discussed by teams of people, and the tools used to share and discuss that code can’t keep up.

But it’s not just a “teams” problem. Have you ever encountered code snippets on websites that are not labeled or classified? Of course you have. We all have!

Developers need tools to help detect, label, format, and enrich their code, and Code Detection API was created for that explicit purpose.

What Is Code Detection API?

Code Detection API is a code processor built as a joint effort by Runtime and APILayer. Runtime’s focus is pioneering the detection, processing, labeling, enrichment, and transforming of code—aka Technical Language Processing. They are responsible for the Pieces application that can instantly save code snippets, extract code from screenshots, and power code auto-completion from those snippets. APILayer not only builds scalable and performant RESTful APIs, but also built the nearly always-on infrastructure those APIs run on.

Code Detection API is simple to use, and it can determine whether a provided string is natural language or technical language (i.e. code). If it detects code, it identifies the language with a high level of accuracy. The best part is Code Detection API’s speed; it only takes approximately 250ms to analyze and identify the provided input. Not only is it extremely fast, but it minimizes on-device computing resources.

So, no matter what software you’re building, be it a documentation site, forum, online code editor, or a developer-focused tool, you need Code Detection API.

Why Use Code Detection API?

Code Detection API is an easy-to-use API that detects, processes, labels, enriches, and transforms code, and you will not find any other service or solution that can provide all of these features:

Fast Code Detection

Code Detection API determines whether a provided text string includes a code snippet using Runtime’s machine learning models.

Accurate Classification

There are a lot of technical languages, and Code Detection API accurately detects 30 languages, including JavaScript, PHP, C#, Ruby, Go, HTML, Markdown, C, and C++.

Enrich and Transform Snippets

Code Detection API can generate syntax-highlighted and truncated versions of your code.

Improve SEO

Use Code Detection API to classify your site’s code snippets, and use the generated metadata to improve indexing, which will enhance your site’s visibility.

Highly Scalable

Code Detection API runs on APILayer’s cloud infrastructure, and it can meet your smallest and largest demands.

Unparalleled Speed

Runtime’s technical language processor analyzes code within milliseconds, and APILayer delivers the response via its lightning-fast infrastructure.

Near 100% Uptime

While nothing is 100%, the uptimes for APILayer’s services are as close as it gets.

How It Works

Getting started with Code Detection API is easy. First, you need to create a Runtime.dev account. After verifying your email address and signing in, you will enter the Runtime Developer Console.

Runtime Developer ConsoleRuntime Developer ConsoleRuntime Developer Console

You need an API key to use Code Detection API, and while you can technically retrieve one for free (for a 14-day trial), you must provide a credit card to start the trial. To obtain a key, click the Discover Products button in the Runtime Developer Console, select your desired plan, and enter your payment information.

After starting your trial, you’ll be redirected back to the Runtime Developer Console, where you will find your API key (and secret), as well as an example request using cURL and its response.

find your API key (and secret), plus an example requestfind your API key (and secret), plus an example requestfind your API key (and secret), plus an example request

Quick Tutorial

Let’s use Code Detection API to evaluate some arbitrary text. It’s important to note, however, that Code Detection API does not support CORS at the time of this writing. Therefore, you cannot directly send requests using JavaScript from within the browser. Therefore, the following code examples will use PHP and the Guzzle HTTP library. Note: the Guzzle library isn’t necessary; you can achieve the same results with cURL functions.

First, create a Guzzle HTTP client object by calling the Client() constructor:

The next step is to send a POST request to Code Detection API. The request should include the apiKey URL parameter set to your API key, and the request body must be in JSON format, as shown in the following code:

This code uses the client object’s request() method and passes three arguments. The first argument is the HTTP method, the second is the URL to send the request to, and the third is an array that specifies the request body. To send JSON, the body array key must be the string json, and its value is an object or array that can be serialized into the following structure:

The iterable property is an array of inputs. Each input is an object that has a value property, and its value is the text that Code Detection API should evaluate. You must include at least one input with the request.

The API responds with a JSON payload. Here is the truncated response from the above request:

truncated response from the above request:truncated response from the above request:truncated response from the above request:

Like the request, the response has a property called iterable, and it is an array that contains the result analysis of the requested inputs. As you’d expect, the indexes for the response iterable array directly correspond with the indexes in the request iterable array.

Each result has naturalLanguage and codeClassification properties, each of which is an object that has a result property. The naturalLanguage object’s result property tells you if the provided input is natural text. The result for codeClassification provides a variety of information if the provided input is code.

Speaking of code, let’s get back to it. Naturally, you need to decode the JSON response into objects:

Pricing

Code Detection API has three subscription plans. Each plan grants you API credits and various features. Obviously, the higher-tier subscriptions grant more API credits and provide extra features.

Code Detection API pricing tableCode Detection API pricing tableCode Detection API pricing table

There is no free plan; however, you do get a free 14-day trial regardless of which subscription you choose. You have to provide a credit card to start the trial. The three plans are:






Plan Description
Basic This plan gets you started with 1,000 API credits and basic analysis of the requested text.
Pro This plan gives 6,000 credits and a more complete analysis of the requested text.
Enterprise This plan gives 36,000 API credits and the same analysis as the Pro plan.
Custom Need even more? Contact them to build a custom plan to fit your needs.

Conclusion

The code that developers must contend with continues to grow exponentially. If you or your team are writing software that lets developers view, share, and discuss code, then you need to integrate Code Detection API into your apps. Getting started is fast and easy. So get started today!